diff --git a/app/build.gradle b/app/build.gradle index c84a4ec..e91a165 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,13 +1,13 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 27 + compileSdkVersion 28 defaultConfig { applicationId "nl.devluuk.sleepywifi" - minSdkVersion 23 - targetSdkVersion 27 - versionCode 8 - versionName '1.7' + minSdkVersion 26 + targetSdkVersion 28 + versionCode 10 + versionName '1.9' testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { @@ -16,20 +16,17 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } - productFlavors { - - } } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') - implementation 'com.android.support:appcompat-v7:27.1.1' + implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' - implementation 'com.android.support:support-v4:27.1.1' + implementation 'com.android.support:support-v4:28.0.0' implementation 'com.karumi:dexter:4.2.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' - implementation 'com.android.support:preference-v7:27.1.1' - implementation 'com.android.support:design:27.1.1' + implementation 'com.android.support:preference-v7:28.0.0' + implementation 'com.android.support:design:28.0.0' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 81a4ed3..b63bdc6 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -8,6 +8,7 @@ + = Build.VERSION_CODES.O) { stopForeground(true); } else { stopSelf(); } - //setPreference(false); - //Log.i(TAG, "BackgroundService is off"); super.onDestroy(); } diff --git a/app/src/main/java/nl/devluuk/sleepywifi/MainActivity.java b/app/src/main/java/nl/devluuk/sleepywifi/MainActivity.java index b600822..a737224 100644 --- a/app/src/main/java/nl/devluuk/sleepywifi/MainActivity.java +++ b/app/src/main/java/nl/devluuk/sleepywifi/MainActivity.java @@ -1,7 +1,6 @@ package nl.devluuk.sleepywifi; import android.app.Activity; -import android.app.ActivityManager; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; @@ -10,6 +9,7 @@ import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.graphics.drawable.Drawable; +import android.net.Uri; import android.os.Bundle; import android.os.PowerManager; import android.preference.PreferenceManager; @@ -38,6 +38,7 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); final ImageView imageButton = findViewById(R.id.powerButton); + powerManager = (PowerManager) this.getSystemService(Context.POWER_SERVICE); checkPrefOnStart(imageButton); imageButton.setOnClickListener(new View.OnClickListener() { @@ -109,7 +110,7 @@ public void playOrPauseService(ImageView image) { setStateText(stateText, stateDesc); } else { - startService(new Intent(this, BackgroundService.class)); + startForegroundService(new Intent(this, BackgroundService.class)); setPreference(true); playIcon = getResources().getDrawable(R.drawable.ic_launcher_round, null); @@ -122,9 +123,16 @@ public void playOrPauseService(ImageView image) { private void checkIgnoringBattery() { String packageName = this.getPackageName(); - if (!powerManager.isIgnoringBatteryOptimizations(packageName)) { - showAlertDialog(this); + PowerManager pm = getSystemService(PowerManager.class); + + if (!pm.isIgnoringBatteryOptimizations(packageName)) { + Intent i = + new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) + .setData(Uri.parse("package:" + packageName)); + + startActivity(i); } + } public void checkPrefOnStart(ImageView image) { @@ -133,11 +141,10 @@ public void checkPrefOnStart(ImageView image) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); prefs.registerOnSharedPreferenceChangeListener(this); - powerManager = (PowerManager) this.getSystemService(Context.POWER_SERVICE); if (checkPrefStatus(getResources().getString(R.string.app_state))) { checkIgnoringBattery(); - startService(new Intent(this, BackgroundService.class)); + startForegroundService(new Intent(this, BackgroundService.class)); playIcon = getResources().getDrawable(R.drawable.ic_launcher_round, null); image.setImageDrawable(playIcon); @@ -153,16 +160,6 @@ public void checkPrefOnStart(ImageView image) { } - private boolean isMyServiceRunning(Class serviceClass) { - ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); - for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { - if (serviceClass.getName().equals(service.service.getClassName())) { - return true; - } - } - return false; - } - private void showAlertDialog(Context context) { final Intent batteryIntent = new Intent(); AlertDialog.Builder batteryDialog = new AlertDialog.Builder(context); @@ -191,10 +188,6 @@ public void onClick(DialogInterface dialog, int id) { alert.show(); } - public boolean getBackgroundStatus() { - return status = isMyServiceRunning(BackgroundService.class); - } - public boolean checkPrefStatus(String key) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); return status = prefs.getBoolean(key, false); diff --git a/app/src/main/java/nl/devluuk/sleepywifi/OnStart.java b/app/src/main/java/nl/devluuk/sleepywifi/OnStart.java index 54d1c76..447f896 100644 --- a/app/src/main/java/nl/devluuk/sleepywifi/OnStart.java +++ b/app/src/main/java/nl/devluuk/sleepywifi/OnStart.java @@ -1,13 +1,9 @@ package nl.devluuk.sleepywifi; import android.content.BroadcastReceiver; -import android.content.ComponentName; import android.content.Context; import android.content.Intent; -import android.content.pm.PackageManager; import android.os.Build; -import android.support.v4.content.ContextCompat; -import android.util.Log; public class OnStart extends BroadcastReceiver { @@ -15,7 +11,7 @@ public class OnStart extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { Intent i = new Intent(context, BackgroundService.class); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(i); } else { context.startService(i); diff --git a/app/src/main/java/nl/devluuk/sleepywifi/ScreenReceiver.java b/app/src/main/java/nl/devluuk/sleepywifi/ScreenReceiver.java index 521e95a..8a3dfa0 100755 --- a/app/src/main/java/nl/devluuk/sleepywifi/ScreenReceiver.java +++ b/app/src/main/java/nl/devluuk/sleepywifi/ScreenReceiver.java @@ -7,32 +7,23 @@ import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.net.wifi.WifiManager; -import android.os.AsyncTask; import android.preference.PreferenceManager; -import android.util.Log; - -import java.lang.ref.WeakReference; -import java.util.concurrent.TimeUnit; public class ScreenReceiver extends BroadcastReceiver { - private WifiManager wifiManager; - private BluetoothAdapter bluetoothAdapter; + WifiManager wifiManager; boolean wifiWasOn = false; boolean bluetoothWasOn = false; boolean bluetoothState; - int delayTime; boolean appState = false; - private static final String TAG = ScreenReceiver.class.getSimpleName(); @Override public void onReceive(final Context context, final Intent intent) { wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); - bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); bluetoothState = prefs.getBoolean(context.getResources().getString(R.string.bluetooth_state), false); appState = prefs.getBoolean(context.getResources().getString(R.string.app_state), false); - delayTime = prefs.getInt(context.getResources().getString(R.string.key_power_off_time), 1); PackageManager pm = context.getPackageManager(); final boolean deviceHasBluetooth = pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH); @@ -40,59 +31,33 @@ public void onReceive(final Context context, final Intent intent) { if (appState) { if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { if (wifiManager.isWifiEnabled()) { - //new Sleep(this).execute(); wifiManager.setWifiEnabled(false); wifiWasOn = true; } else { wifiWasOn = false; } - if (bluetoothState) { - if (deviceHasBluetooth) { - if (bluetoothAdapter.isEnabled()) { - bluetoothAdapter.disable(); - bluetoothWasOn = true; - } else { - bluetoothWasOn = false; - } + if (bluetoothState && deviceHasBluetooth) { + if (bluetoothAdapter.isEnabled()) { + bluetoothAdapter.disable(); + bluetoothWasOn = true; + } else { + bluetoothWasOn = false; } + } } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { if (wifiWasOn) { wifiManager.setWifiEnabled(true); } - if (bluetoothState) { - if (deviceHasBluetooth) { - if (bluetoothWasOn) { - bluetoothAdapter.enable(); - } + if (bluetoothState && deviceHasBluetooth) { + if (bluetoothWasOn) { + bluetoothAdapter.enable(); } } } } } - private class Sleep extends AsyncTask { - - private WeakReference activityReference; - - // only retain a weak reference to the activity - Sleep(ScreenReceiver context) { - activityReference = new WeakReference<>(context); - } - - protected Void doInBackground(Void... params) { - try { - Log.v(TAG, "Sleeping......."); - TimeUnit.SECONDS.sleep(delayTime); - wifiManager.setWifiEnabled(false); - Log.v(TAG, "Wifi is offfff"); - } catch (InterruptedException e) { - e.printStackTrace(); - } - return null; - } - } - } diff --git a/app/src/main/java/nl/devluuk/sleepywifi/SettingsActivity.java b/app/src/main/java/nl/devluuk/sleepywifi/SettingsActivity.java index e16b2c6..b658aed 100755 --- a/app/src/main/java/nl/devluuk/sleepywifi/SettingsActivity.java +++ b/app/src/main/java/nl/devluuk/sleepywifi/SettingsActivity.java @@ -1,9 +1,9 @@ package nl.devluuk.sleepywifi; import android.app.ActionBar; +import android.os.Bundle; import android.support.v4.app.NavUtils; import android.support.v7.app.AppCompatActivity; -import android.os.Bundle; import android.view.MenuItem; public class SettingsActivity extends AppCompatActivity { diff --git a/app/src/main/res/values-nl-rNL/strings.xml b/app/src/main/res/values-nl-rNL/strings.xml index db787b9..fb0c6b4 100644 --- a/app/src/main/res/values-nl-rNL/strings.xml +++ b/app/src/main/res/values-nl-rNL/strings.xml @@ -18,13 +18,10 @@ Standaard Zet Bluetooth uit in slaapmodus Zet Bluetooth slapen aan - Hou de Wifi aan tijdens slaapmodus Wifi staat Wanneer de telefoon in slaapmodus is Nee Ja Deze app werkt niet goed todat je deze app toestemming geeft om batterij optimalizatie te negeren Verzoek negeer batterij-optimalisatie voor deze app - Stel het uitgaan van de WiFi uit - Kies hoe lang het duurt voordat de wifi uit gaat \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0eb8063..67d2b0e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -23,13 +23,12 @@ SleepyWifi Info License Author - This Android app brings back the \'turn wifi off when the phone is in sleepmode\' option for Android 8.1 or newer. + This Android app brings back the \'turn wifi off when the phone is in sleepmode\' option for Android 8.1 and Android 9.0. SleepyWifi is licensed under GPL-3.0 or later. SleepyWifi is developed by Devluuk Settings - General @@ -38,36 +37,4 @@ Enable Bluetooth Sleeping Turn Bluetooth off when the phone is in sleepmode - - Keep Wifi on during sleep - - wifi_state - - - Always - Only when plugged in - Never - - - 1 - 0 - -1 - - - power_off - - Delay Wifi turning off - Choose how long the delay is for turning off the wifi - - 1 second - 5 seconds - 20 seconds - 60 seconds - - - 1 - 5 - 20 - 60 - \ No newline at end of file diff --git a/app/src/main/res/xml/pref_visualizer.xml b/app/src/main/res/xml/pref_visualizer.xml index d212559..39ca791 100644 --- a/app/src/main/res/xml/pref_visualizer.xml +++ b/app/src/main/res/xml/pref_visualizer.xml @@ -3,14 +3,6 @@ - - - - - - - - + android:summary="@string/pref_description_enable_bluetooth" + android:title="@string/pref_bluetooth_title" /> \ No newline at end of file diff --git a/build.gradle b/build.gradle index fdfb867..a64add5 100644 --- a/build.gradle +++ b/build.gradle @@ -1,14 +1,13 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - + repositories { google() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.5.2' - + classpath 'com.android.tools.build:gradle:3.5.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files