move battery optimization request to base activity
This commit is contained in:
parent
e9015a5541
commit
6a194a7952
4 changed files with 75 additions and 62 deletions
|
|
@ -1,14 +1,7 @@
|
||||||
package com.dkanada.gramophone.activities;
|
package com.dkanada.gramophone.activities;
|
||||||
|
|
||||||
import android.app.AlertDialog;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
|
||||||
import android.os.Build;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.PowerManager;
|
|
||||||
import android.provider.Settings;
|
|
||||||
|
|
||||||
import androidx.annotation.RequiresApi;
|
|
||||||
|
|
||||||
import com.dkanada.gramophone.App;
|
import com.dkanada.gramophone.App;
|
||||||
import com.dkanada.gramophone.R;
|
import com.dkanada.gramophone.R;
|
||||||
|
|
@ -38,46 +31,13 @@ public class SplashActivity extends AbsBaseActivity {
|
||||||
@Override
|
@Override
|
||||||
public void onPause() {
|
public void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
overridePendingTransition(0, R.anim.fade_delay);
|
overridePendingTransition(0, R.anim.fade_quick);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && detectBatteryOptimization()) {
|
|
||||||
showBatteryOptimizationDialog();
|
|
||||||
} else {
|
|
||||||
login();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
|
||||||
private boolean detectBatteryOptimization() {
|
|
||||||
String packageName = getPackageName();
|
|
||||||
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
|
|
||||||
|
|
||||||
return !pm.isIgnoringBatteryOptimizations(packageName);
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
|
||||||
private void showBatteryOptimizationDialog() {
|
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(SplashActivity.this);
|
|
||||||
builder.setMessage(R.string.battery_optimizations_message)
|
|
||||||
.setTitle(R.string.battery_optimizations_title)
|
|
||||||
.setNegativeButton(R.string.ignore, (dialog, id) -> login())
|
|
||||||
.setPositiveButton(R.string.disable, (dialog, id) -> openPowerSettings())
|
|
||||||
.show();
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
|
||||||
private void openPowerSettings() {
|
|
||||||
Intent intent = new Intent();
|
|
||||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
||||||
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
|
|
||||||
startActivity(intent);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void login() {
|
|
||||||
Context context = this;
|
Context context = this;
|
||||||
User user = App.getDatabase().userDao().getUser(PreferenceUtil.getInstance(this).getUser());
|
User user = App.getDatabase().userDao().getUser(PreferenceUtil.getInstance(this).getUser());
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,17 +2,25 @@ package com.dkanada.gramophone.activities.base;
|
||||||
|
|
||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
|
import android.content.Intent;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.PowerManager;
|
||||||
|
import android.provider.Settings;
|
||||||
|
import android.view.View;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.annotation.RequiresApi;
|
import androidx.annotation.RequiresApi;
|
||||||
import androidx.core.app.ActivityCompat;
|
import androidx.core.app.ActivityCompat;
|
||||||
|
|
||||||
|
import com.dkanada.gramophone.activities.MainActivity;
|
||||||
import com.dkanada.gramophone.util.NavigationUtil;
|
import com.dkanada.gramophone.util.NavigationUtil;
|
||||||
import com.dkanada.gramophone.R;
|
import com.dkanada.gramophone.R;
|
||||||
|
import com.google.android.material.snackbar.Snackbar;
|
||||||
|
import com.kabouzeid.appthemehelper.ThemeStore;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -37,8 +45,31 @@ public abstract class AbsBaseActivity extends AbsThemeActivity {
|
||||||
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
|
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
|
||||||
super.onPostCreate(savedInstanceState);
|
super.onPostCreate(savedInstanceState);
|
||||||
|
|
||||||
if (!hasPermissions()) {
|
if (!getClass().isInstance(MainActivity.class)) {
|
||||||
requestPermissions();
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
AlertDialog.Builder builder = new AlertDialog.Builder(this)
|
||||||
|
.setNegativeButton(R.string.ignore, (dialog, id) -> showWarning());
|
||||||
|
|
||||||
|
if (!checkBatteryOptimization()) {
|
||||||
|
builder.setMessage(R.string.battery_optimizations_message)
|
||||||
|
.setTitle(R.string.battery_optimizations_title)
|
||||||
|
.setPositiveButton(R.string.disable, (dialog, id) -> requestBatteryOptimization());
|
||||||
|
|
||||||
|
new Handler().postDelayed(builder::show, 2000);
|
||||||
|
} else if (permissions.size() != 0 && ActivityCompat.shouldShowRequestPermissionRationale(this, permissions.get(0))) {
|
||||||
|
builder.setMessage(getPermissionDeniedMessage())
|
||||||
|
.setTitle(R.string.permissions_denied)
|
||||||
|
.setPositiveButton(R.string.action_grant, (dialog, id) -> requestPermissions());
|
||||||
|
|
||||||
|
new Handler().postDelayed(builder::show, 2000);
|
||||||
|
} else if (!hasPermissions()) {
|
||||||
|
builder.setMessage(getPermissionDeniedMessage())
|
||||||
|
.setTitle(R.string.permissions_denied)
|
||||||
|
.setPositiveButton(R.string.action_settings, (dialog, id) -> NavigationUtil.openSettings(this));
|
||||||
|
|
||||||
|
new Handler().postDelayed(builder::show, 2000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -46,11 +77,16 @@ public abstract class AbsBaseActivity extends AbsThemeActivity {
|
||||||
@TargetApi(Build.VERSION_CODES.M)
|
@TargetApi(Build.VERSION_CODES.M)
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
|
||||||
if (hasPermissions() != allowed) {
|
if (hasPermissions() != allowed) {
|
||||||
super.recreate();
|
super.recreate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected View getPermissionWindow() {
|
||||||
|
return getWindow().getDecorView();
|
||||||
|
}
|
||||||
|
|
||||||
protected List<String> getPermissionsToRequest() {
|
protected List<String> getPermissionsToRequest() {
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
@ -59,6 +95,31 @@ public abstract class AbsBaseActivity extends AbsThemeActivity {
|
||||||
return getString(R.string.permissions_denied);
|
return getString(R.string.permissions_denied);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void showWarning() {
|
||||||
|
Snackbar.make(getPermissionWindow(), getPermissionDeniedMessage(), Snackbar.LENGTH_SHORT)
|
||||||
|
.setAction(R.string.ignore, view -> { })
|
||||||
|
.setActionTextColor(ThemeStore.accentColor(this))
|
||||||
|
.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||||
|
private void requestBatteryOptimization() {
|
||||||
|
Intent intent = new Intent();
|
||||||
|
|
||||||
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
|
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
|
||||||
|
|
||||||
|
startActivity(intent);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||||
|
private boolean checkBatteryOptimization() {
|
||||||
|
String packageName = getPackageName();
|
||||||
|
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
|
||||||
|
|
||||||
|
return pm.isIgnoringBatteryOptimizations(packageName);
|
||||||
|
}
|
||||||
|
|
||||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||||
protected void requestPermissions() {
|
protected void requestPermissions() {
|
||||||
requestPermissions(permissions.toArray(new String[0]), PERMISSION_REQUEST);
|
requestPermissions(permissions.toArray(new String[0]), PERMISSION_REQUEST);
|
||||||
|
|
@ -84,25 +145,11 @@ public abstract class AbsBaseActivity extends AbsThemeActivity {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < permissions.length; i++) {
|
for (int result : results) {
|
||||||
String permission = permissions[i];
|
if (result != PackageManager.PERMISSION_GRANTED) {
|
||||||
int result = results[i];
|
showWarning();
|
||||||
|
return;
|
||||||
if (result == PackageManager.PERMISSION_GRANTED) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(this)
|
|
||||||
.setMessage(getPermissionDeniedMessage())
|
|
||||||
.setTitle(R.string.permissions_denied)
|
|
||||||
.setNegativeButton(R.string.ignore, (dialog, which) -> { })
|
|
||||||
.setPositiveButton(R.string.action_settings, (dialog, id) -> NavigationUtil.openSettings(this));
|
|
||||||
|
|
||||||
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
|
|
||||||
builder.setPositiveButton(R.string.action_grant, (dialog, id) -> requestPermissions());
|
|
||||||
}
|
|
||||||
|
|
||||||
builder.show();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import android.content.IntentFilter;
|
||||||
import android.content.ServiceConnection;
|
import android.content.ServiceConnection;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
import android.view.View;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
|
|
@ -185,6 +186,11 @@ public abstract class AbsMusicServiceActivity extends AbsBaseActivity implements
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected View getPermissionWindow() {
|
||||||
|
return findViewById(R.id.content_container);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getPermissionDeniedMessage() {
|
protected String getPermissionDeniedMessage() {
|
||||||
return getString(R.string.permission_external_storage_denied);
|
return getString(R.string.permission_external_storage_denied);
|
||||||
|
|
|
||||||
|
|
@ -205,7 +205,7 @@
|
||||||
<string name="playing_notification_description">The playing notification provides actions for play/pause etc.</string>
|
<string name="playing_notification_description">The playing notification provides actions for play/pause etc.</string>
|
||||||
<string name="playing_notification_name">Playing Notification</string>
|
<string name="playing_notification_name">Playing Notification</string>
|
||||||
<string name="you_have_to_select_at_least_one_category">You have to select at least one category.</string>
|
<string name="you_have_to_select_at_least_one_category">You have to select at least one category.</string>
|
||||||
<string name="permissions_denied">Permissions denied.</string>
|
<string name="permissions_denied">Permissions denied</string>
|
||||||
<string name="permission_external_storage_denied">Permission to access external storage denied.</string>
|
<string name="permission_external_storage_denied">Permission to access external storage denied.</string>
|
||||||
|
|
||||||
<string name="sort_order_ascending">Ascending</string>
|
<string name="sort_order_ascending">Ascending</string>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue