Skip to content

Commit

Permalink
Merge pull request #16 from DevLuuk/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
DevLuuk committed Oct 29, 2019
2 parents 70698eb + 3b0621d commit 91b5931
Show file tree
Hide file tree
Showing 15 changed files with 170 additions and 156 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ captures/

# IntelliJ
*.iml
.idea/
.idea/workspace.xml
.idea/tasks.xml
.idea/gradle.xml
Expand Down Expand Up @@ -63,3 +64,6 @@ fastlane/Preview.html
fastlane/screenshots
fastlane/test_output
fastlane/readme.md

.DS_Store
app/release
19 changes: 10 additions & 9 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 27
compileSdkVersion 28
defaultConfig {
applicationId "nl.devluuk.sleepywifi"
minSdkVersion 23
targetSdkVersion 27
versionCode 6
versionName '1.5'
targetSdkVersion 28
versionCode 7
versionName '1.6'
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {

}
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.0.2'
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'
}
4 changes: 1 addition & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nl.devluuk.sleepywifi">

<!-- <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> -->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Expand All @@ -11,7 +10,6 @@
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-feature android:name="android.hardware.bluetooth" android:required="false" />


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand All @@ -26,7 +24,7 @@
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Expand Down
19 changes: 17 additions & 2 deletions app/src/main/java/nl/devluuk/sleepywifi/AboutActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,35 @@

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class AboutActivity extends Activity {
String url = "https://github.com/DevLuuk";

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

TextView authorText = findViewById(R.id.author_text);
authorText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
}

/**
Expand All @@ -37,7 +52,7 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.home){
if (id == R.id.home) {
NavUtils.navigateUpFromSameTask(this);
}
return super.onOptionsItemSelected(item);
Expand Down
12 changes: 0 additions & 12 deletions app/src/main/java/nl/devluuk/sleepywifi/BackgroundService.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

public class BackgroundService extends Service {

private static final String TAG = BackgroundService.class.getSimpleName();
protected ScreenReceiver screenReciever;

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

private class DismissNotification extends AsyncTask<Void, Void, Void> {
Expand All @@ -69,21 +66,12 @@ private class DismissNotification extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
try {
TimeUnit.SECONDS.sleep(5);
//Thread.sleep(5000);
stopForeground(true);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}

protected void onProgressUpdate(Void... progress) {

}

protected void onPostExecute(Void result) {

}
}


Expand Down
57 changes: 48 additions & 9 deletions app/src/main/java/nl/devluuk/sleepywifi/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
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;
import android.provider.Settings;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity implements SharedPreferences.OnSharedPreferenceChangeListener {
Expand All @@ -26,6 +32,7 @@ public class MainActivity extends Activity implements SharedPreferences.OnShared
public Boolean status;
public Drawable playIcon;
public Drawable grayIcon;
private PowerManager powerManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -52,7 +59,7 @@ protected void onResume() {
}

@Override
protected void onPause(){
protected void onPause() {
super.onPause();
PreferenceManager.getDefaultSharedPreferences(this)
.registerOnSharedPreferenceChangeListener(this);
Expand Down Expand Up @@ -92,11 +99,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
public void playOrPauseService(ImageView image) {
final TextView stateText = findViewById(R.id.OnOffText);
final TextView stateDesc = findViewById(R.id.OnOffDescription);

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

setPreference(false);

grayIcon = getResources().getDrawable(R.drawable.ic_launcher_round_gray, null);
Expand All @@ -107,7 +112,6 @@ public void playOrPauseService(ImageView image) {

} else {
startService(new Intent(this, BackgroundService.class));

setPreference(true);

playIcon = getResources().getDrawable(R.drawable.ic_launcher_round, null);
Expand All @@ -118,17 +122,24 @@ public void playOrPauseService(ImageView image) {
}
}

private void checkIgnoringBattery() {
String packageName = this.getPackageName();
if (!powerManager.isIgnoringBatteryOptimizations(packageName)) {
showAlertDialog(this);
}
}

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);
powerManager = (PowerManager) this.getSystemService(Context.POWER_SERVICE);

if (checkPrefStatus(getResources().getString(R.string.app_state))) {
checkIgnoringBattery();
startService(new Intent(this, BackgroundService.class));
//setPreference(true);
//Toast.makeText(this, "Service started", Toast.LENGTH_SHORT).show();
playIcon = getResources().getDrawable(R.drawable.ic_launcher_round, null);

image.setImageDrawable(playIcon);
Expand All @@ -154,6 +165,34 @@ private boolean isMyServiceRunning(Class<?> serviceClass) {
return false;
}

private void showAlertDialog(Context context) {
final Intent batteryIntent = new Intent();
AlertDialog.Builder batteryDialog = new AlertDialog.Builder(context);
batteryDialog.setTitle(R.string.request_battery_opt);
batteryDialog.setMessage(R.string.warning_battery_opt);
batteryDialog.setCancelable(true);

batteryDialog.setPositiveButton(
R.string.yes,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
batteryIntent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
startActivity(batteryIntent);
}
});

batteryDialog.setNegativeButton(
R.string.no,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});

AlertDialog alert = batteryDialog.create();
alert.show();
}

public boolean getBackgroundStatus() {
return status = isMyServiceRunning(BackgroundService.class);
}
Expand Down Expand Up @@ -206,11 +245,11 @@ private Drawable makeGrayIcon(Drawable icon, int colorID) {
*/
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(getString(R.string.bluetooth_state))){
if (key.equals(getString(R.string.bluetooth_state))) {
sharedPreferences.getBoolean(key, false);
}
if (key.equals("app_state")) {
sharedPreferences.getBoolean(key, true);
}
sharedPreferences.getBoolean(key, true);
}
}
}
Loading

0 comments on commit 91b5931

Please sign in to comment.