11package com .android .mycax .floatingvolume .services ;
22
3+ import android .Manifest ;
4+ import android .animation .LayoutTransition ;
35import android .annotation .SuppressLint ;
46import android .app .Service ;
57import android .content .BroadcastReceiver ;
68import android .content .Context ;
79import android .content .Intent ;
810import android .content .IntentFilter ;
911import android .content .SharedPreferences ;
12+ import android .content .pm .PackageManager ;
1013import android .content .res .Configuration ;
14+ import android .content .res .TypedArray ;
1115import android .graphics .PixelFormat ;
1216import android .graphics .Point ;
1317import android .media .AudioManager ;
1620import android .os .IBinder ;
1721import android .preference .PreferenceManager ;
1822import android .support .annotation .Nullable ;
23+ import android .support .v4 .content .ContextCompat ;
24+ import android .telephony .TelephonyManager ;
1925import android .util .DisplayMetrics ;
2026import android .view .Gravity ;
2127import android .view .LayoutInflater ;
2228import android .view .MotionEvent ;
2329import android .view .View ;
30+ import android .view .ViewGroup ;
2431import android .view .WindowManager ;
2532import android .view .animation .Animation ;
2633import android .view .animation .AnimationUtils ;
2734import android .widget .ImageView ;
2835import android .widget .SeekBar ;
36+ import android .widget .TextView ;
2937
3038import com .android .mycax .floatingvolume .R ;
3139import com .android .mycax .floatingvolume .audio .AudioVolumeObserver ;
@@ -46,13 +54,15 @@ public class FloatingVolumeService extends Service implements FloatingViewListen
4654 private FloatingViewManager mFloatingViewManager ;
4755 private AudioVolumeObserver mAudioVolumeObserverMedia , mAudioVolumeObserverVoiceCall , mAudioVolumeObserverRinger , mAudioVolumeObserverAlarm ;
4856 private SeekBar mediaControl , ringerControl , alarmControl , voiceCallControl ;
49- private BroadcastReceiver RingerModeReceiver ;
50- private boolean isDarkThemeEnabled , isDisableStaticUiEnabled , isUseLastPosition , isBounceEnabled ;
57+ private BroadcastReceiver RingerModeReceiver , InCallModeReceiver ;
58+ private TelephonyManager telephonyManager ;
59+ private boolean isDisableStaticUiEnabled , isUseLastPosition , isBounceEnabled , isVoiceCallRecieverRegistered ;
5160 private int x_init_cord , y_init_cord , x_init_margin , y_init_margin , style ;
5261 private final Point szWindow = new Point ();
5362 private SharedPreferences .Editor editor ;
5463 private SharedPreferences sharedPref ;
5564 private Set <String > seekbarSelections ;
65+ private Animation fab_open_0_to_1 , fab_close_1_to_0 ;
5666 private static int OVERLAY_TYPE ;
5767
5868 static {
@@ -85,6 +95,8 @@ public int onStartCommand(Intent intent, int flags, int startId) {
8595 return START_STICKY ;
8696 }
8797
98+ fab_open_0_to_1 = AnimationUtils .loadAnimation (this , R .anim .fab_open_0_to_1 );
99+ fab_close_1_to_0 = AnimationUtils .loadAnimation (this , R .anim .fab_close_1_to_0 );
88100 sharedPref = PreferenceManager .getDefaultSharedPreferences (this );
89101 final DisplayMetrics metrics = new DisplayMetrics ();
90102 final WindowManager windowManager = (WindowManager ) getSystemService (Context .WINDOW_SERVICE );
@@ -110,8 +122,9 @@ public void onClick(View v) {
110122 private void expandView (LayoutInflater inflater , DisplayMetrics displayMetrics ) {
111123 mWindowManager = (WindowManager ) getSystemService (WINDOW_SERVICE );
112124 audioManager = (AudioManager ) getSystemService (Context .AUDIO_SERVICE );
125+ telephonyManager = (TelephonyManager )getSystemService (Context .TELEPHONY_SERVICE );
113126
114- isDarkThemeEnabled = sharedPref .getBoolean (Constants .PREF_ENABLE_DARK_MODE , false );
127+ boolean isDarkThemeEnabled = sharedPref .getBoolean (Constants .PREF_ENABLE_DARK_MODE , false );
115128 isDisableStaticUiEnabled = sharedPref .getBoolean (Constants .PREF_DISABLE_FIXED_UI , false );
116129 isUseLastPosition = sharedPref .getBoolean (Constants .PREF_SAVE_LAST_POSITION , false );
117130 isBounceEnabled = sharedPref .getBoolean (Constants .PREF_ENABLE_BOUNCE , false );
@@ -132,6 +145,8 @@ private void expandView(LayoutInflater inflater, DisplayMetrics displayMetrics)
132145 private void addFloatingWidgetView (LayoutInflater inflater , DisplayMetrics displayMetrics ) {
133146 getWindowManagerDefaultDisplay ();
134147 mFloatingWidgetView = inflater .inflate (getDialogLayout (), null , false );
148+ ((ViewGroup ) mFloatingWidgetView .findViewById (R .id .root_container )).getLayoutTransition ()
149+ .enableTransitionType (LayoutTransition .CHANGING );
135150
136151 final WindowManager .LayoutParams params = new WindowManager .LayoutParams (
137152 WindowManager .LayoutParams .WRAP_CONTENT ,
@@ -181,17 +196,57 @@ private void implementVolumeFeatures() {
181196 R .id .SeekBarVoiceCallRotator , R .id .ImageVoiceCall , R .id .linearLayoutVoiceCall );
182197
183198 change_ringer_mode = mFloatingWidgetView .findViewById (R .id .imageViewModeSwitch );
184- final Animation fab_open = AnimationUtils . loadAnimation ( this , R . anim . fab_open_0_to_1 );
199+
185200 RingerModeReceiver = new BroadcastReceiver () {
186201 @ Override
187202 public void onReceive (Context context , Intent intent ) {
188203 change_ringer_mode .setImageResource (getCurrentRingerModeDrawable ());
189- change_ringer_mode .startAnimation (fab_open );
204+ change_ringer_mode .startAnimation (fab_open_0_to_1 );
190205 }
191206 };
192- IntentFilter filter = new IntentFilter (
207+ final IntentFilter filterRingerChanged = new IntentFilter (
193208 AudioManager .RINGER_MODE_CHANGED_ACTION );
194- registerReceiver (RingerModeReceiver , filter );
209+ registerReceiver (RingerModeReceiver , filterRingerChanged );
210+
211+ InCallModeReceiver = new BroadcastReceiver () {
212+ @ Override
213+ public void onReceive (Context context , Intent intent ) {
214+ assert telephonyManager != null ;
215+ TextView textViewVoiceCall = mFloatingWidgetView .findViewById (R .id .textViewVoiceCall );
216+ if (seekbarSelections .contains (Constants .SEEKBAR_VOICE_CALL ) && telephonyManager .getCallState () == TelephonyManager .CALL_STATE_OFFHOOK ) {
217+ textViewVoiceCall .setVisibility (View .VISIBLE );
218+ textViewVoiceCall .startAnimation (fab_open_0_to_1 );
219+ if (style == 3 ) {
220+ mFloatingWidgetView .findViewById (R .id .SeekBarVoiceCallRotator ).setVisibility (View .VISIBLE );
221+ mFloatingWidgetView .findViewById (R .id .SeekBarVoiceCallRotator ).startAnimation (fab_open_0_to_1 );
222+ mFloatingWidgetView .findViewById (R .id .ImageVoiceCall ).setVisibility (View .VISIBLE );
223+ mFloatingWidgetView .findViewById (R .id .ImageVoiceCall ).startAnimation (fab_open_0_to_1 );
224+ } else {
225+ mFloatingWidgetView .findViewById (R .id .linearLayoutVoiceCall ).setVisibility (View .VISIBLE );
226+ mFloatingWidgetView .findViewById (R .id .linearLayoutVoiceCall ).startAnimation (fab_open_0_to_1 );
227+ }
228+ } else {
229+ textViewVoiceCall .setVisibility (View .GONE );
230+ textViewVoiceCall .startAnimation (fab_close_1_to_0 );
231+ if (style == 3 ) {
232+ mFloatingWidgetView .findViewById (R .id .SeekBarVoiceCallRotator ).setVisibility (View .GONE );
233+ mFloatingWidgetView .findViewById (R .id .SeekBarVoiceCallRotator ).startAnimation (fab_close_1_to_0 );
234+ mFloatingWidgetView .findViewById (R .id .ImageVoiceCall ).setVisibility (View .GONE );
235+ mFloatingWidgetView .findViewById (R .id .ImageVoiceCall ).startAnimation (fab_close_1_to_0 );
236+ } else {
237+ mFloatingWidgetView .findViewById (R .id .linearLayoutVoiceCall ).setVisibility (View .GONE );
238+ mFloatingWidgetView .findViewById (R .id .linearLayoutVoiceCall ).startAnimation (fab_close_1_to_0 );
239+ }
240+ }
241+ }
242+ };
243+ final IntentFilter filterPhoneStateChanged = new IntentFilter (
244+ TelephonyManager .ACTION_PHONE_STATE_CHANGED );
245+ if (ContextCompat .checkSelfPermission (this , Manifest .permission .READ_PHONE_STATE ) == PackageManager .PERMISSION_GRANTED ) {
246+ registerReceiver (InCallModeReceiver , filterPhoneStateChanged );
247+ isVoiceCallRecieverRegistered = true ;
248+ } else isVoiceCallRecieverRegistered = false ;
249+
195250 change_ringer_mode .setOnClickListener (this );
196251 }
197252
@@ -288,35 +343,32 @@ public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
288343 }
289344
290345 private int getCurrentRingerModeDrawable () {
346+ TypedArray attrs ;
291347 switch (audioManager .getRingerMode ()) {
292348 case AudioManager .RINGER_MODE_NORMAL :
293- return isDarkThemeEnabled ? R .drawable .ic_volume_up_white_24dp : R .drawable .ic_volume_up_black_24dp ;
349+ attrs = getTheme ().obtainStyledAttributes (new int []{R .attr .ringer_normal });
350+ return attrs .getResourceId (0 , 0 );
294351 case AudioManager .RINGER_MODE_VIBRATE :
295- return isDarkThemeEnabled ? R .drawable .ic_vibration_white_24dp : R .drawable .ic_vibration_black_24dp ;
352+ attrs = getTheme ().obtainStyledAttributes (new int []{R .attr .ringer_vibrate });
353+ return attrs .getResourceId (0 , 0 );
296354 case AudioManager .RINGER_MODE_SILENT :
297- return isDarkThemeEnabled ? R .drawable .ic_do_not_disturb_on_white_24dp : R .drawable .ic_do_not_disturb_on_black_24dp ;
355+ attrs = getTheme ().obtainStyledAttributes (new int []{R .attr .ringer_silent });
356+ return attrs .getResourceId (0 , 0 );
298357 }
299358 return -1 ;
300359 }
301360
302361 private void setNewRingerMode () {
303362 int ringerMode = audioManager .getRingerMode ();
304- Animation fab_open = AnimationUtils .loadAnimation (this , R .anim .fab_open_0_to_1 );
305363 switch (ringerMode ) {
306364 case AudioManager .RINGER_MODE_NORMAL :
307365 audioManager .setRingerMode (AudioManager .RINGER_MODE_VIBRATE );
308- change_ringer_mode .setImageResource (isDarkThemeEnabled ? R .drawable .ic_vibration_white_24dp : R .drawable .ic_vibration_black_24dp );
309- change_ringer_mode .startAnimation (fab_open );
310366 break ;
311367 case AudioManager .RINGER_MODE_VIBRATE :
312368 audioManager .setRingerMode (AudioManager .RINGER_MODE_SILENT );
313- change_ringer_mode .setImageResource (isDarkThemeEnabled ? R .drawable .ic_do_not_disturb_on_white_24dp : R .drawable .ic_do_not_disturb_on_black_24dp );
314- change_ringer_mode .startAnimation (fab_open );
315369 break ;
316370 case AudioManager .RINGER_MODE_SILENT :
317371 audioManager .setRingerMode (AudioManager .RINGER_MODE_NORMAL );
318- change_ringer_mode .setImageResource (isDarkThemeEnabled ? R .drawable .ic_volume_up_white_24dp : R .drawable .ic_volume_up_black_24dp );
319- change_ringer_mode .startAnimation (fab_open );
320372 break ;
321373 }
322374 }
@@ -340,6 +392,10 @@ public void onClick(View v) {
340392 mAudioVolumeObserverVoiceCall .unregister ();
341393 }
342394 unregisterReceiver (RingerModeReceiver );
395+ if (isVoiceCallRecieverRegistered ) {
396+ unregisterReceiver (InCallModeReceiver );
397+ isVoiceCallRecieverRegistered = false ;
398+ }
343399 break ;
344400 case R .id .imageViewModeSwitch :
345401 setNewRingerMode ();
0 commit comments