Skip to content

Commit

Permalink
FloatingVolume: VoiceCall seekbar improvements and code cleanup
Browse files Browse the repository at this point in the history
* Don't hardcode ringer mode drawables
* Adjust about app section
* Remove some redundant code
* Properly handle voice call seekbar show/hide event (Needs READ_PHONE_STATE permission)
* Manage new permission

Signed-off-by: Adam Myczkowski <[email protected]>
  • Loading branch information
Adam Myczkowski committed May 11, 2018
1 parent 1d5b68e commit ebbdf86
Show file tree
Hide file tree
Showing 17 changed files with 170 additions and 43 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ android {
applicationId "com.android.mycax.floatingvolume"
minSdkVersion min_sdk_version
targetSdkVersion compile_sdk_version
versionCode 8
versionName "1.0.7 beta"
versionCode 9
versionName "1.0.8 beta"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -72,4 +72,5 @@ dependencies {
implementation 'com.github.BaselHorany:DualButton:1.1.1'
implementation 'com.github.recruit-lifestyle:FloatingView:2.3.1'
implementation 'com.github.jrvansuita:MaterialAbout:0.2.3'
implementation 'com.karumi:dexter:4.2.0'
}
1 change: 1 addition & 0 deletions app/lint.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
<ignore path="src/main/res/mipmap-hdpi/ic_launcher.png" />
<ignore path="src/main/res/mipmap-mdpi/ic_launcher.png" />
<ignore path="src/main/res/mipmap-xhdpi/ic_launcher.png" />
<ignore path="src/main/res/mipmap-xhdpi/ic_launcher.png" />
</issue>
</lint>
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<!-- Permission required to draw floating widget over other apps -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application
android:allowBackup="true"
Expand Down
6 changes: 2 additions & 4 deletions app/src/main/java/com/android/musicfx/SeekBarRotator.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.android.musicfx;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
/*
Expand All @@ -36,6 +35,7 @@ public SeekBarRotator(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@SuppressWarnings("SuspiciousNameCombination")
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final View child = getChildAt(0);
Expand All @@ -62,9 +62,7 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
// place the child below this view, so it rotates into view
int mywidth = r - l;
int myheight = b - t;
int childwidth = myheight;
int childheight = mywidth;
child.layout(0, myheight, childwidth, myheight + childheight);
child.layout(0, myheight, myheight, myheight + mywidth);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ private void loadAbout() {
.setName(R.string.dev_name)
.setSubTitle(R.string.dev_sub)
.setLinksColumnsCount(3)
.addFacebookLink("mycax6")
.addTwitterLink("Adam_Myczkowski")
.addInstagramLink("mycax6")
.addGooglePlusLink("112043897899708921734")
.addYoutubeChannelLink("UCxQZUuxF0N7Aj0SNlLcEpgw")
.addEmailLink("[email protected]")
.addGitHubLink("MyczkowskiAdam")
.addWebsiteLink("https://myczkowskiadam.github.io/FloatingVolume/")
.addLink(R.mipmap.github, R.string.git_project, "https://github.com/MyczkowskiAdam/FloatingVolume")
.addFacebookLink(R.string.name_fb_ig)
.addTwitterLink(R.string.name_twitter)
.addInstagramLink(R.string.name_fb_ig)
.addGooglePlusLink(R.string.name_googleplus)
.addYoutubeChannelLink(R.string.name_youtube)
.addEmailLink(R.string.name_email)
.addGitHubLink(R.string.name_github)
.addWebsiteLink(R.string.url_fv_website)
.addLink(R.mipmap.github, R.string.git_project, getString(R.string.url_fv_repo))
.setVersionNameAsAppSubTitle()
.addShareAction(R.string.app_name)
.addShareAction(R.string.app_name, R.string.url_xdalabs_link)
.setActionsColumnsCount(2)
.addFeedbackAction("[email protected]")
.addFeedbackAction(R.string.name_email)
.setWrapScrollView(true)
.setShowAsCard(true);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.android.mycax.floatingvolume;

import android.Manifest;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.NotificationManager;
import android.content.Context;
Expand All @@ -17,9 +19,16 @@
import com.android.mycax.floatingvolume.utils.AppUtils;
import com.android.mycax.floatingvolume.utils.Constants;
import com.basel.DualButton.DualButton;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.single.PermissionListener;

import java.util.Objects;

@SuppressLint("ExportedPreferenceActivity")
@SuppressWarnings("deprecation")
public class MainActivity extends AppCompatPreferenceActivity implements SwitchPreference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener, DualButton.OnDualClickListener {
private DualButton FloatingService;
Expand Down Expand Up @@ -107,6 +116,18 @@ private void checkPermissions() {
startActivityForResult(intent, Constants.NOTIFICATION_POLICY_PERMISSION_REQUEST);
}
}

Dexter.withActivity(this)
.withPermission(Manifest.permission.READ_PHONE_STATE)
.withListener(new PermissionListener() {
@Override public void onPermissionGranted(PermissionGrantedResponse response) {/* ... */}
@Override public void onPermissionDenied(PermissionDeniedResponse response) {
Toast.makeText(getApplicationContext(), R.string.app_permission_denied, Toast.LENGTH_SHORT).show();
}
@Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
token.continuePermissionRequest();
}
}).check();
}

@TargetApi(Build.VERSION_CODES.M)
Expand All @@ -116,7 +137,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (Settings.canDrawOverlays(this) && Objects.requireNonNull(notificationManager).isNotificationPolicyAccessGranted()) {
initializeView();
} else {
Toast.makeText(this, R.string.app_permission_denied, Toast.LENGTH_SHORT).show();
Toast.makeText(this, R.string.app_permission_denied, Toast.LENGTH_LONG).show();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AudioVolumeContentObserver extends ContentObserver {
private int mLastVolume;
private final AudioVolumeObserver audioVolumeObserver;

public AudioVolumeContentObserver(
AudioVolumeContentObserver(
@NonNull Handler handler,
@NonNull AudioManager audioManager,
int audioStreamType,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.android.mycax.floatingvolume.services;

import android.Manifest;
import android.animation.LayoutTransition;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.media.AudioManager;
Expand All @@ -16,16 +20,20 @@
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.android.mycax.floatingvolume.R;
import com.android.mycax.floatingvolume.audio.AudioVolumeObserver;
Expand All @@ -46,13 +54,15 @@ public class FloatingVolumeService extends Service implements FloatingViewListen
private FloatingViewManager mFloatingViewManager;
private AudioVolumeObserver mAudioVolumeObserverMedia, mAudioVolumeObserverVoiceCall, mAudioVolumeObserverRinger, mAudioVolumeObserverAlarm;
private SeekBar mediaControl, ringerControl, alarmControl, voiceCallControl;
private BroadcastReceiver RingerModeReceiver;
private boolean isDarkThemeEnabled, isDisableStaticUiEnabled, isUseLastPosition, isBounceEnabled;
private BroadcastReceiver RingerModeReceiver, InCallModeReceiver;
private TelephonyManager telephonyManager;
private boolean isDisableStaticUiEnabled, isUseLastPosition, isBounceEnabled, isVoiceCallRecieverRegistered;
private int x_init_cord, y_init_cord, x_init_margin, y_init_margin, style;
private final Point szWindow = new Point();
private SharedPreferences.Editor editor;
private SharedPreferences sharedPref;
private Set<String> seekbarSelections;
private Animation fab_open_0_to_1, fab_close_1_to_0;
private static int OVERLAY_TYPE;

static {
Expand Down Expand Up @@ -85,6 +95,8 @@ public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}

fab_open_0_to_1 = AnimationUtils.loadAnimation(this, R.anim.fab_open_0_to_1);
fab_close_1_to_0 = AnimationUtils.loadAnimation(this, R.anim.fab_close_1_to_0);
sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
final DisplayMetrics metrics = new DisplayMetrics();
final WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Expand All @@ -110,8 +122,9 @@ public void onClick(View v) {
private void expandView(LayoutInflater inflater, DisplayMetrics displayMetrics) {
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

isDarkThemeEnabled = sharedPref.getBoolean(Constants.PREF_ENABLE_DARK_MODE, false);
boolean isDarkThemeEnabled = sharedPref.getBoolean(Constants.PREF_ENABLE_DARK_MODE, false);
isDisableStaticUiEnabled = sharedPref.getBoolean(Constants.PREF_DISABLE_FIXED_UI, false);
isUseLastPosition = sharedPref.getBoolean(Constants.PREF_SAVE_LAST_POSITION, false);
isBounceEnabled = sharedPref.getBoolean(Constants.PREF_ENABLE_BOUNCE, false);
Expand All @@ -132,6 +145,8 @@ private void expandView(LayoutInflater inflater, DisplayMetrics displayMetrics)
private void addFloatingWidgetView(LayoutInflater inflater, DisplayMetrics displayMetrics) {
getWindowManagerDefaultDisplay();
mFloatingWidgetView = inflater.inflate(getDialogLayout(), null, false);
((ViewGroup) mFloatingWidgetView.findViewById(R.id.root_container)).getLayoutTransition()
.enableTransitionType(LayoutTransition.CHANGING);

final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
Expand Down Expand Up @@ -181,17 +196,57 @@ private void implementVolumeFeatures() {
R.id.SeekBarVoiceCallRotator, R.id.ImageVoiceCall, R.id.linearLayoutVoiceCall);

change_ringer_mode = mFloatingWidgetView.findViewById(R.id.imageViewModeSwitch);
final Animation fab_open = AnimationUtils.loadAnimation(this, R.anim.fab_open_0_to_1);

RingerModeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
change_ringer_mode.setImageResource(getCurrentRingerModeDrawable());
change_ringer_mode.startAnimation(fab_open);
change_ringer_mode.startAnimation(fab_open_0_to_1);
}
};
IntentFilter filter = new IntentFilter(
final IntentFilter filterRingerChanged = new IntentFilter(
AudioManager.RINGER_MODE_CHANGED_ACTION);
registerReceiver(RingerModeReceiver, filter);
registerReceiver(RingerModeReceiver, filterRingerChanged);

InCallModeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
assert telephonyManager != null;
TextView textViewVoiceCall = mFloatingWidgetView.findViewById(R.id.textViewVoiceCall);
if (seekbarSelections.contains(Constants.SEEKBAR_VOICE_CALL) && telephonyManager.getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) {
textViewVoiceCall.setVisibility(View.VISIBLE);
textViewVoiceCall.startAnimation(fab_open_0_to_1);
if (style == 3) {
mFloatingWidgetView.findViewById(R.id.SeekBarVoiceCallRotator).setVisibility(View.VISIBLE);
mFloatingWidgetView.findViewById(R.id.SeekBarVoiceCallRotator).startAnimation(fab_open_0_to_1);
mFloatingWidgetView.findViewById(R.id.ImageVoiceCall).setVisibility(View.VISIBLE);
mFloatingWidgetView.findViewById(R.id.ImageVoiceCall).startAnimation(fab_open_0_to_1);
} else {
mFloatingWidgetView.findViewById(R.id.linearLayoutVoiceCall).setVisibility(View.VISIBLE);
mFloatingWidgetView.findViewById(R.id.linearLayoutVoiceCall).startAnimation(fab_open_0_to_1);
}
} else {
textViewVoiceCall.setVisibility(View.GONE);
textViewVoiceCall.startAnimation(fab_close_1_to_0);
if (style == 3) {
mFloatingWidgetView.findViewById(R.id.SeekBarVoiceCallRotator).setVisibility(View.GONE);
mFloatingWidgetView.findViewById(R.id.SeekBarVoiceCallRotator).startAnimation(fab_close_1_to_0);
mFloatingWidgetView.findViewById(R.id.ImageVoiceCall).setVisibility(View.GONE);
mFloatingWidgetView.findViewById(R.id.ImageVoiceCall).startAnimation(fab_close_1_to_0);
} else {
mFloatingWidgetView.findViewById(R.id.linearLayoutVoiceCall).setVisibility(View.GONE);
mFloatingWidgetView.findViewById(R.id.linearLayoutVoiceCall).startAnimation(fab_close_1_to_0);
}
}
}
};
final IntentFilter filterPhoneStateChanged = new IntentFilter(
TelephonyManager.ACTION_PHONE_STATE_CHANGED);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
registerReceiver(InCallModeReceiver, filterPhoneStateChanged);
isVoiceCallRecieverRegistered = true;
} else isVoiceCallRecieverRegistered = false;

change_ringer_mode.setOnClickListener(this);
}

Expand Down Expand Up @@ -288,35 +343,32 @@ public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
}

private int getCurrentRingerModeDrawable() {
TypedArray attrs;
switch (audioManager.getRingerMode()) {
case AudioManager.RINGER_MODE_NORMAL:
return isDarkThemeEnabled ? R.drawable.ic_volume_up_white_24dp : R.drawable.ic_volume_up_black_24dp;
attrs = getTheme().obtainStyledAttributes(new int[]{R.attr.ringer_normal});
return attrs.getResourceId(0, 0);
case AudioManager.RINGER_MODE_VIBRATE:
return isDarkThemeEnabled ? R.drawable.ic_vibration_white_24dp : R.drawable.ic_vibration_black_24dp;
attrs = getTheme().obtainStyledAttributes(new int[]{R.attr.ringer_vibrate});
return attrs.getResourceId(0, 0);
case AudioManager.RINGER_MODE_SILENT:
return isDarkThemeEnabled ? R.drawable.ic_do_not_disturb_on_white_24dp : R.drawable.ic_do_not_disturb_on_black_24dp;
attrs = getTheme().obtainStyledAttributes(new int[]{R.attr.ringer_silent});
return attrs.getResourceId(0, 0);
}
return -1;
}

private void setNewRingerMode() {
int ringerMode = audioManager.getRingerMode();
Animation fab_open = AnimationUtils.loadAnimation(this, R.anim.fab_open_0_to_1);
switch (ringerMode) {
case AudioManager.RINGER_MODE_NORMAL:
audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
change_ringer_mode.setImageResource(isDarkThemeEnabled ? R.drawable.ic_vibration_white_24dp : R.drawable.ic_vibration_black_24dp);
change_ringer_mode.startAnimation(fab_open);
break;
case AudioManager.RINGER_MODE_VIBRATE:
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
change_ringer_mode.setImageResource(isDarkThemeEnabled ? R.drawable.ic_do_not_disturb_on_white_24dp : R.drawable.ic_do_not_disturb_on_black_24dp);
change_ringer_mode.startAnimation(fab_open);
break;
case AudioManager.RINGER_MODE_SILENT:
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
change_ringer_mode.setImageResource(isDarkThemeEnabled ? R.drawable.ic_volume_up_white_24dp : R.drawable.ic_volume_up_black_24dp);
change_ringer_mode.startAnimation(fab_open);
break;
}
}
Expand All @@ -340,6 +392,10 @@ public void onClick(View v) {
mAudioVolumeObserverVoiceCall.unregister();
}
unregisterReceiver(RingerModeReceiver);
if (isVoiceCallRecieverRegistered) {
unregisterReceiver(InCallModeReceiver);
isVoiceCallRecieverRegistered = false;
}
break;
case R.id.imageViewModeSwitch:
setNewRingerMode();
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/res/anim/fab_close_1_to_0.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<scale
android:duration="300"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0.0"
android:toYScale="0.0" />
<alpha
android:duration="300"
android:fromAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="0.0" />
</set>
15 changes: 11 additions & 4 deletions app/src/main/res/layout/activity_about.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<tools:LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".AboutActivity">

<FrameLayout
android:id="@+id/about"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AboutActivity"
tools:ignore="MergeRootFrame" />
android:layout_height="match_parent">

</FrameLayout>

</tools:LinearLayout>
Loading

0 comments on commit ebbdf86

Please sign in to comment.