Skip to content

Commit

Permalink
Merge pull request #11 from DevLuuk/develop
Browse files Browse the repository at this point in the history
Updated the app to version 1.4
  • Loading branch information
DevLuuk committed Jan 21, 2019
2 parents 9f240e8 + 76f1e02 commit 8c1fe5c
Show file tree
Hide file tree
Showing 18 changed files with 280 additions and 58 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# SleepyWifi
This Android app brings back the 'turn wifi off when the phone is in sleepmode' option for Android 8.1.
This Android app brings back the 'turn wifi off when the phone is in sleepmode' option for Android 8.1 or higher.
After you have opened the app the apps service runs in the background. The app works for Android devices with version 6.0 or higher.

## Why this app?
Google has removed the 'turn wifi off when the phone is in sleepmode' options in Android 8.1 and I was a fan of that option so I thought: 'Why don't I make that option in an android app for android 8.1 devices'.
Google has removed the 'turn wifi off when the phone is in sleepmode' options in Android 8.1 and I was a fan of that option so I thought: 'Why don't I make that option in an android app for android 8.1 or newer devices'.

## How to install?
1. Download on [Google Play](https://play.google.com/store/apps/details?id=nl.devluuk.sleepywifi)
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "nl.devluuk.sleepywifi"
minSdkVersion 23
targetSdkVersion 27
versionCode 4
versionName '1.3'
versionCode 5
versionName '1.4'
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
17 changes: 14 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.QUICKBOOT_POWERON" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<application
android:allowBackup="true"
Expand All @@ -17,8 +19,8 @@
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
android:launchMode="singleTop">
android:launchMode="singleTop"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down Expand Up @@ -48,7 +50,16 @@
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
android:value=".MainActivity" />
</activity>
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"
android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="nl.devluuk.sleepywifi.MainActivity" />
</activity>
</application>

Expand Down
11 changes: 10 additions & 1 deletion app/src/main/java/nl/devluuk/sleepywifi/BackgroundService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.support.v4.app.NotificationCompat;
import android.util.Log;

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

Expand Down Expand Up @@ -51,12 +52,20 @@ public void onCreate() {
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(this.screenReciever, filter);
new DismissNotification().execute();
new DismissNotification(this).execute();
// remove start notification
//stopForeground(false);
}

private class DismissNotification extends AsyncTask<Void, Void, Void> {

private WeakReference<BackgroundService> activityReference;

// only retain a weak reference to the activity
DismissNotification(BackgroundService context) {
activityReference = new WeakReference<>(context);
}

protected Void doInBackground(Void... params) {
try {
TimeUnit.SECONDS.sleep(5);
Expand Down
96 changes: 61 additions & 35 deletions app/src/main/java/nl/devluuk/sleepywifi/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.drawable.Drawable;
Expand All @@ -23,10 +17,9 @@
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {
public class MainActivity extends Activity implements SharedPreferences.OnSharedPreferenceChangeListener {

private static final String TAG = MainActivity.class.getSimpleName();
public String state;
Expand Down Expand Up @@ -54,15 +47,23 @@ public void onClick(View v) {
protected void onResume() {
super.onResume();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
status = prefs.getBoolean("app_state", true);
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
status = prefs.getBoolean(getResources().getString(R.string.app_state), true);
}

// @Override
// protected void onDestroy() {
// super.onDestroy();
// PreferenceManager.getDefaultSharedPreferences(this)
// .unregisterOnSharedPreferenceChangeListener(this);
// }
@Override
protected void onPause(){
super.onPause();
PreferenceManager.getDefaultSharedPreferences(this)
.registerOnSharedPreferenceChangeListener(this);
}

@Override
protected void onDestroy() {
super.onDestroy();
PreferenceManager.getDefaultSharedPreferences(this)
.unregisterOnSharedPreferenceChangeListener(this);
}


@Override
Expand All @@ -75,6 +76,11 @@ public boolean onCreateOptionsMenu(Menu menu) {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int menuItemSelected = item.getItemId();
if (menuItemSelected == R.id.action_settings) {
Intent settingsActivity = new Intent(this, SettingsActivity.class);
startActivity(settingsActivity);
return true;
}
if (menuItemSelected == R.id.action_about) {
Intent aboutActivity = new Intent(this, AboutActivity.class);
startActivity(aboutActivity);
Expand All @@ -85,11 +91,10 @@ public boolean onOptionsItemSelected(MenuItem item) {

public void playOrPauseService(ImageView image) {
final TextView stateText = findViewById(R.id.OnOffText);
//SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
//prefs.registerOnSharedPreferenceChangeListener(this);
final TextView stateDesc = findViewById(R.id.OnOffDescription);

// Check the current state inside the sharedprefs
if (checkPrefStatus()) {
if (checkPrefStatus(getResources().getString(R.string.app_state))) {
stopService(new Intent(this, BackgroundService.class));

setPreference(false);
Expand All @@ -98,7 +103,7 @@ public void playOrPauseService(ImageView image) {
makeGrayIcon(grayIcon, 0);
image.setImageDrawable(grayIcon);
// Update current state text
setStateText(stateText);
setStateText(stateText, stateDesc);

} else {
startService(new Intent(this, BackgroundService.class));
Expand All @@ -109,14 +114,18 @@ public void playOrPauseService(ImageView image) {

image.setImageDrawable(playIcon);
// Update current state text
setStateText(stateText);
setStateText(stateText, stateDesc);
}
}

public void checkPrefOnStart(ImageView image) {
final TextView stateText = findViewById(R.id.OnOffText);
final TextView stateDesc = findViewById(R.id.OnOffDescription);

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);

if (checkPrefStatus()) {
if (checkPrefStatus(getResources().getString(R.string.app_state))) {
startService(new Intent(this, BackgroundService.class));
//setPreference(true);
//Toast.makeText(this, "Service started", Toast.LENGTH_SHORT).show();
Expand All @@ -125,12 +134,12 @@ public void checkPrefOnStart(ImageView image) {
image.setImageDrawable(playIcon);
Log.i(TAG, "service is started");
// Update current state text
setStateText(stateText);
setStateText(stateText, stateDesc);
} else {
grayIcon = getResources().getDrawable(R.drawable.ic_launcher_round_gray, null);
makeGrayIcon(grayIcon, 0);
image.setImageDrawable(grayIcon);
setStateText(stateText);
setStateText(stateText, stateDesc);
}

}
Expand All @@ -149,26 +158,30 @@ public boolean getBackgroundStatus() {
return status = isMyServiceRunning(BackgroundService.class);
}

public boolean checkPrefStatus() {
public boolean checkPrefStatus(String key) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
return status = prefs.getBoolean("app_state", true);
return status = prefs.getBoolean(key, true);
}

public void setPreference(boolean status) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
editor.putBoolean("app_state", status);
editor.putBoolean(getResources().getString(R.string.app_state), status);
editor.apply();
}

private void setStateText(TextView stateText) {
if (checkPrefStatus()) {
private void setStateText(TextView stateText, TextView stateDesc) {
String stateDescription;
if (checkPrefStatus(getResources().getString(R.string.app_state))) {
state = getResources().getString(R.string.on);
stateDescription = getResources().getString(R.string.off);
} else {
state = getResources().getString(R.string.off);
stateDescription = getResources().getString(R.string.on);
}
stateText.setText(getResources().getString(R.string.app_state) + " " + state);
stateText.setText(getResources().getString(R.string.app_state_title) + " " + state);
stateDesc.setText(getResources().getString(R.string.app_state_desc1) + " " + stateDescription + " " + getResources().getString(R.string.app_state_desc2));
}


Expand All @@ -181,10 +194,23 @@ private Drawable makeGrayIcon(Drawable icon, int colorID) {
return icon;
}

// @Override
// public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// if (key.equals("app_state")) {
// sharedPreferences.getBoolean(key, true);
// }
// }
/**
* Called when a shared preference is changed, added, or removed. This
* may be called even if a preference is set to its existing value.
*
* <p>This callback will be run on your main thread.
*
* @param sharedPreferences The {@link SharedPreferences} that received
* the change.
* @param key The key of the preference that was changed, added, or
*/
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(getString(R.string.bluetooth_state))){
sharedPreferences.getBoolean(key, false);
}
if (key.equals("app_state")) {
sharedPreferences.getBoolean(key, true);
}
}
}
33 changes: 28 additions & 5 deletions app/src/main/java/nl/devluuk/sleepywifi/ScreenReceiver.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,59 @@
package nl.devluuk.sleepywifi;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

public class ScreenReceiver extends BroadcastReceiver {

private WifiManager wifiManager;
private BluetoothAdapter bluetoothAdapter;
boolean wifiWasOn = false;
boolean bluetoothWasOn = false;
boolean bluetoothState;
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();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
bluetoothState = prefs.getBoolean(context.getResources().getString(R.string.bluetooth_state), false);
//boolean wifiEnabled = wifiManager.isWifiEnabled();

if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
if (wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
wifiWasOn = true;

} else {
wifiWasOn = false;
}
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON) && wifiWasOn) {
wifiManager.setWifiEnabled(true);
if (bluetoothState) {
if (bluetoothAdapter.isEnabled()) {
bluetoothAdapter.disable();
Log.v(TAG, "Bluetooth is Sleeping");
bluetoothWasOn = true;
} else {
bluetoothWasOn = false;
}
}
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
if (wifiWasOn) {
wifiManager.setWifiEnabled(true);
}
if (bluetoothState) {
if (bluetoothWasOn) {
bluetoothAdapter.enable();
}
}
}
}

Expand Down
30 changes: 30 additions & 0 deletions app/src/main/java/nl/devluuk/sleepywifi/SettingsActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package nl.devluuk.sleepywifi;

import android.app.ActionBar;
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 {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
ActionBar actionBar = this.getActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.home) {
NavUtils.navigateUpFromSameTask(this);
}
return super.onOptionsItemSelected(item);

}
}
Loading

0 comments on commit 8c1fe5c

Please sign in to comment.