Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for handling SYSTEM_ALERT_WINDOW permission on Marshmallow #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {
defaultConfig {
applicationId "com.emmaguy.cleanstatusbar"
minSdkVersion 15
targetSdkVersion 22
targetSdkVersion 23
versionCode 17
versionName "1.1.3"
}
Expand Down
73 changes: 68 additions & 5 deletions app/src/main/java/com/emmaguy/cleanstatusbar/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
package com.emmaguy.cleanstatusbar;

import android.annotation.TargetApi;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
import android.provider.Settings;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;

import com.emmaguy.cleanstatusbar.prefs.TimePreference;

public class MainActivity extends AppCompatActivity {
public static int OVERLAY_PERMISSION_REQ_CODE = 1000;

Switch masterSwitch;

@Override
protected void onResume() {
super.onResume();

if (!canDrawOverlays()){
masterSwitch.setChecked(false);
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -28,15 +47,25 @@ protected void onCreate(Bundle savedInstanceState) {
getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
}

@TargetApi(Build.VERSION_CODES.M)
private boolean canDrawOverlays() {
return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || Settings.canDrawOverlays(getApplicationContext());
}

private void initSwitch() {
Switch masterSwitch = new Switch(this);
masterSwitch.setChecked(CleanStatusBarService.isRunning());
masterSwitch = new Switch(this);
masterSwitch.setChecked(CleanStatusBarService.isRunning() && canDrawOverlays());
masterSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Intent service = new Intent(MainActivity.this, CleanStatusBarService.class);
if (b) {
startService(service);
if(canDrawOverlays()) {
startService(service);
} else {
compoundButton.setChecked(false);
showPermissionSnackBar();
}
} else {
stopService(service);
}
Expand All @@ -47,8 +76,42 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
final ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
lp.rightMargin = getResources().getDimensionPixelSize(R.dimen.master_switch_margin_right);
bar.setCustomView(masterSwitch, lp);
bar.setDisplayShowCustomEnabled(true);
if (bar != null ) {
bar.setCustomView(masterSwitch, lp);
bar.setDisplayShowCustomEnabled(true);
}
}

@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
boolean permissionEnabled = Settings.canDrawOverlays(this);

masterSwitch.setChecked(permissionEnabled);

if (!permissionEnabled) {
showPermissionSnackBar();
}
}
}

private void showPermissionSnackBar() {
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), R.string.snack_bar_system_alert_window_rationale, Snackbar.LENGTH_INDEFINITE);
snackbar.setAction(R.string.snack_bar_system_alert_window_action, new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onClick(View view) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
try {
startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
} catch (ActivityNotFoundException e) {
// do nothing
}
}
});
snackbar.show();
}

public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<string name="empty">Empty</string>
<string name="network_icon_info">Signal icon only seen when WiFi is off</string>
<string name="gps">GPS</string>
<string name="snack_bar_system_alert_window_rationale">On Android 6.0+ this app requires permission to
draw to the status bar</string>
<string name="snack_bar_system_alert_window_action">Settings</string>


<!-- Notification -->
<string name="clean_status_bar_is_running">Clean status bar is running</string>
Expand Down