Skip to content

Commit

Permalink
Fixed merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
DevLuuk committed Dec 24, 2019
2 parents 4111af2 + f6ec66d commit 19d9728
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 148 deletions.
21 changes: 9 additions & 12 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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'
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<uses-permission android:name="android.permission.QUICKBOOT_POWERON"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-feature android:name="android.hardware.bluetooth" android:required="false" />

<application
Expand Down
19 changes: 6 additions & 13 deletions app/src/main/java/nl/devluuk/sleepywifi/BackgroundService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,17 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Build;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import java.lang.ref.WeakReference;
import java.net.URL;
import java.util.concurrent.TimeUnit;

public class BackgroundService extends Service {

protected ScreenReceiver screenReciever;
protected ScreenReceiver screenReceiver;

@Override
public void onCreate() {
Expand All @@ -47,10 +43,10 @@ public void onCreate() {


final IntentFilter filter = new IntentFilter();
this.screenReciever = new ScreenReceiver();
this.screenReceiver = new ScreenReceiver();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(this.screenReciever, filter);
registerReceiver(this.screenReceiver, filter);
new DismissNotification(this).execute();
}

Expand All @@ -77,18 +73,15 @@ protected Void doInBackground(Void... params) {

@Override
public void onDestroy() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (screenReciever != null) {
unregisterReceiver(screenReciever);
screenReciever = null;
if (screenReceiver != null) {
unregisterReceiver(screenReceiver);
screenReceiver = null;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
stopForeground(true);
} else {
stopSelf();
}
//setPreference(false);
//Log.i(TAG, "BackgroundService is off");
super.onDestroy();
}

Expand Down
33 changes: 13 additions & 20 deletions app/src/main/java/nl/devluuk/sleepywifi/MainActivity.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 1 addition & 5 deletions app/src/main/java/nl/devluuk/sleepywifi/OnStart.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
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 {

@Override
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);
Expand Down
59 changes: 12 additions & 47 deletions app/src/main/java/nl/devluuk/sleepywifi/ScreenReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,92 +7,57 @@
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);

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<Void, Void, Void> {

private WeakReference<ScreenReceiver> 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;
}
}

}


Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/res/values-nl-rNL/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
<string name="pref_header_general">Standaard</string>
<string name="pref_description_enable_bluetooth">Zet Bluetooth uit in slaapmodus</string>
<string name="pref_bluetooth_title">Zet Bluetooth slapen aan</string>
<string name="pref_keep_wifi_on">Hou de Wifi aan tijdens slaapmodus</string>
<string name="app_state_desc1">Wifi staat</string>
<string name="app_state_desc2">Wanneer de telefoon in slaapmodus is</string>
<string name="no">Nee</string>
<string name="yes">Ja</string>
<string name="warning_battery_opt">Deze app werkt niet goed todat je deze app toestemming geeft om batterij optimalizatie te negeren</string>
<string name="request_battery_opt">Verzoek negeer batterij-optimalisatie voor deze app</string>
<string name="pref_delay_power_off">Stel het uitgaan van de WiFi uit</string>
<string name="pref_delay_power_off_detail">Kies hoe lang het duurt voordat de wifi uit gaat</string>
</resources>
Loading

0 comments on commit 19d9728

Please sign in to comment.