15
15
* limitations under the License.
16
16
*/
17
17
18
+ import android .annotation .TargetApi ;
18
19
import android .content .Context ;
19
20
import android .content .res .Resources .Theme ;
20
21
import android .media .AudioDeviceCallback ;
@@ -29,66 +30,77 @@ public class AudioDeviceSpinner extends Spinner {
29
30
30
31
private static final int AUTO_SELECT_DEVICE_ID = 0 ;
31
32
private static final String TAG = AudioDeviceSpinner .class .getName ();
32
- private int mDirectionType = AudioManager .GET_DEVICES_OUTPUTS ;
33
+ private int mDirectionType ;
34
+ private AudioDeviceAdapter mDeviceAdapter ;
35
+ private AudioManager mAudioManager ;
33
36
34
37
public AudioDeviceSpinner (Context context ){
35
38
super (context );
39
+ setup (context );
36
40
}
37
41
38
42
public AudioDeviceSpinner (Context context , int mode ){
39
43
super (context , mode );
44
+ setup (context );
40
45
}
41
46
42
47
public AudioDeviceSpinner (Context context , AttributeSet attrs ){
43
48
super (context , attrs );
49
+ setup (context );
44
50
}
45
51
46
52
public AudioDeviceSpinner (Context context , AttributeSet attrs , int defStyleAttr ){
47
53
super (context , attrs , defStyleAttr );
54
+ setup (context );
48
55
}
49
56
50
57
public AudioDeviceSpinner (Context context , AttributeSet attrs , int defStyleAttr , int mode ){
51
58
super (context , attrs , defStyleAttr , mode );
59
+ setup (context );
52
60
}
53
61
54
62
public AudioDeviceSpinner (Context context , AttributeSet attrs , int defStyleAttr ,
55
63
int defStyleRes , int mode ){
56
64
super (context , attrs , defStyleAttr , defStyleRes , mode );
65
+ setup (context );
57
66
}
58
67
public AudioDeviceSpinner (Context context , AttributeSet attrs , int defStyleAttr ,
59
68
int defStyleRes , int mode , Theme popupTheme ){
60
69
super (context , attrs , defStyleAttr , defStyleRes , mode , popupTheme );
70
+ setup (context );
61
71
}
62
72
63
- public void setDirectionType (int directionType ){
64
- this .mDirectionType = directionType ;
65
- setupAdapter ();
66
- }
73
+ private void setup (Context context ){
74
+ mAudioManager = (AudioManager ) context .getSystemService (Context .AUDIO_SERVICE );
67
75
68
- private void setupAdapter (){
69
-
70
- Context context = getContext ();
71
- AudioManager audioManager = (AudioManager ) context .getSystemService (Context .AUDIO_SERVICE );
72
-
73
- final AudioDeviceAdapter deviceAdapter = new AudioDeviceAdapter (context );
74
- setAdapter (deviceAdapter );
76
+ mDeviceAdapter = new AudioDeviceAdapter (context );
77
+ setAdapter (mDeviceAdapter );
75
78
76
79
// Add a default entry to the list and select it
77
- deviceAdapter .add (new AudioDeviceListEntry (AUTO_SELECT_DEVICE_ID ,
80
+ mDeviceAdapter .add (new AudioDeviceListEntry (AUTO_SELECT_DEVICE_ID ,
78
81
context .getString (R .string .auto_select )));
79
82
setSelection (0 );
83
+ }
84
+
85
+ @ TargetApi (23 )
86
+ public void setDirectionType (int directionType ){
87
+ this .mDirectionType = directionType ;
88
+ setupAudioDeviceCallback ();
89
+ }
90
+
91
+ @ TargetApi (23 )
92
+ private void setupAudioDeviceCallback (){
80
93
81
- // Listen for changes
82
94
// Note that we will immediately receive a call to onDevicesAdded with the list of
83
95
// devices which are currently connected.
84
- audioManager .registerAudioDeviceCallback (new AudioDeviceCallback () {
96
+ mAudioManager .registerAudioDeviceCallback (new AudioDeviceCallback () {
85
97
@ Override
86
98
public void onAudioDevicesAdded (AudioDeviceInfo [] addedDevices ) {
87
99
88
100
List <AudioDeviceListEntry > deviceList =
89
101
AudioDeviceListEntry .createListFrom (addedDevices , mDirectionType );
90
102
if (deviceList .size () > 0 ){
91
- deviceAdapter .addAll (deviceList );
103
+ mDeviceAdapter .addAll (deviceList );
92
104
}
93
105
}
94
106
@@ -97,7 +109,7 @@ public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) {
97
109
List <AudioDeviceListEntry > deviceList =
98
110
AudioDeviceListEntry .createListFrom (removedDevices , mDirectionType );
99
111
for (AudioDeviceListEntry entry : deviceList ){
100
- deviceAdapter .remove (entry );
112
+ mDeviceAdapter .remove (entry );
101
113
}
102
114
}
103
115
}, null );
0 commit comments