Skip to content

Commit

Permalink
Merge pull request #13 from DevLuuk/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
DevLuuk committed Jan 24, 2019
2 parents 8c1fe5c + 2dde0a0 commit 535c44d
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 16 deletions.
8 changes: 5 additions & 3 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 5
versionName '1.4'
versionCode 6
versionName '1.5'
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -22,11 +22,13 @@ android {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-v4:27.1.1'
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'
}
14 changes: 9 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
<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" />
<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.QUICKBOOT_POWERON"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<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"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true">
android:supportsRtl="true"
>
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden"
Expand Down Expand Up @@ -47,7 +50,8 @@
<activity
android:name=".AboutActivity"
android:label="@string/action_about"
android:parentActivityName=".MainActivity">
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
Expand Down
26 changes: 18 additions & 8 deletions app/src/main/java/nl/devluuk/sleepywifi/ScreenReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.wifi.WifiManager;
import android.preference.PreferenceManager;
import android.util.Log;
Expand All @@ -26,6 +27,10 @@ public void onReceive(final Context context, final Intent intent) {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
bluetoothState = prefs.getBoolean(context.getResources().getString(R.string.bluetooth_state), false);

PackageManager pm = context.getPackageManager();
final boolean deviceHasBluetooth = pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);

//boolean wifiEnabled = wifiManager.isWifiEnabled();

if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Expand All @@ -37,21 +42,26 @@ public void onReceive(final Context context, final Intent intent) {
wifiWasOn = false;
}
if (bluetoothState) {
if (bluetoothAdapter.isEnabled()) {
bluetoothAdapter.disable();
Log.v(TAG, "Bluetooth is Sleeping");
bluetoothWasOn = true;
} else {
bluetoothWasOn = false;
if (deviceHasBluetooth) {
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();
if (deviceHasBluetooth) {
if (bluetoothWasOn) {
bluetoothAdapter.enable();
}
}
}
}
Expand Down
78 changes: 78 additions & 0 deletions app/src/main/java/nl/devluuk/sleepywifi/SettingsActivity.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
package nl.devluuk.sleepywifi;

import android.Manifest;
import android.app.ActionBar;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v14.preference.SwitchPreference;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.preference.Preference;
import android.view.MenuItem;
import android.widget.Toast;

import com.karumi.dexter.Dexter;
import com.karumi.dexter.MultiplePermissionsReport;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.DexterError;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.PermissionRequestErrorListener;
import com.karumi.dexter.listener.multi.MultiplePermissionsListener;

import java.util.List;

public class SettingsActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -27,4 +46,63 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);

}
// TODO Fully implement onPreferenceListener
// public static void bluetoothListener(SwitchPreference bluetoothSwitch){
// if (bluetoothSwitch != null) {
// bluetoothSwitch.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
// @Override
// public boolean onPreferenceChange(Preference arg0, Object isBluetoothOnObject) {
// boolean isBluetoothOn = (Boolean) isBluetoothOnObject;
// if (isBluetoothOn) {
// requestBluetoothPermission();
// }
// return true;
// }
// });
// }
// }

private void requestBluetoothPermission(){
Dexter.withActivity(this).withPermissions(
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN
).withListener(new MultiplePermissionsListener() {
@Override
public void onPermissionsChecked(MultiplePermissionsReport report) {
if (report.areAllPermissionsGranted()){
Toast.makeText(getApplicationContext(), "All permissions are granted", Toast.LENGTH_SHORT).show();
}

if (report.isAnyPermissionPermanentlyDenied()){
Toast.makeText(getApplicationContext(), "Not all permissions are granted", Toast.LENGTH_SHORT).show();
showWarningDialog();
}
}

@Override
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
token.continuePermissionRequest();
}
}).withErrorListener(new PermissionRequestErrorListener() {
@Override
public void onError(DexterError error) {
Toast.makeText(getApplicationContext(), "Error occurred ", Toast.LENGTH_LONG).show();
}
}).onSameThread().check();
}

private void showWarningDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(SettingsActivity.this);
builder.setTitle("Need Permissions");
builder.setMessage("This app need permissions to use this feature.");
builder.setPositiveButton("Oke", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}


}
6 changes: 6 additions & 0 deletions app/src/main/java/nl/devluuk/sleepywifi/SettingsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.support.v7.preference.PreferenceScreen;

import nl.devluuk.sleepywifi.SettingsActivity;

public class SettingsFragment extends PreferenceFragmentCompat {
/**
* Called during {@link #onCreate(Bundle)} to supply the preferences for this fragment.
Expand All @@ -19,5 +23,7 @@ public class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.pref_visualizer);


}
}

0 comments on commit 535c44d

Please sign in to comment.