Skip to content

Commit

Permalink
Add notice about not being able to change WiFi state while in airplan…
Browse files Browse the repository at this point in the history
…e mode on Android 8.1+
  • Loading branch information
j4velin committed Feb 1, 2018
1 parent c667052 commit 1374ac5
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
package="de.j4velin.wifiAutoOff"
xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="internalOnly"
android:versionCode="176"
android:versionName="1.7.6">
android:versionCode="1770"
android:versionName="1.7.7">

<uses-sdk
android:minSdkVersion="14"
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/de/j4velin/wifiAutoOff/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public enum Type {
LOCATION_ENTERED(R.drawable.event_location_entered), TIMER(R.drawable.event_timer),
AIRPLANE_MODE(R.drawable.event_airplane_mode), SCREEN_OFF(R.drawable.event_display_off),
UNLOCKED(R.drawable.event_display_on), AC_CONNECTED(R.drawable.event_ac_connected),
AC_DISCONNECTED(R.drawable.event_ac_disconnected), HOTSPOT(R.drawable.event_hotspot);
AC_DISCONNECTED(R.drawable.event_ac_disconnected), HOTSPOT(R.drawable.event_hotspot),
ERROR(R.drawable.event_error);

public final int drawable;

Expand Down
22 changes: 22 additions & 0 deletions src/main/java/de/j4velin/wifiAutoOff/NoticePreference.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package de.j4velin.wifiAutoOff;

import android.content.Context;
import android.graphics.Color;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class NoticePreference extends Preference {

public NoticePreference(final Context context, final AttributeSet attrs) {
super(context, attrs);
}

@Override
protected View onCreateView(final ViewGroup parent) {
View v = super.onCreateView(parent);
v.setBackgroundColor(Color.parseColor("#66ccff"));
return v;
}
}
20 changes: 19 additions & 1 deletion src/main/java/de/j4velin/wifiAutoOff/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,25 @@ public boolean onPreferenceClick(final Preference preference) {
}
} else if (!wm.isWifiEnabled()) {
try {
wm.setWifiEnabled(true);
boolean success = wm.setWifiEnabled(true);
if (!success) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
try {
if (APILevel17Wrapper
.isAirplaneModeOn(getApplicationContext())) {
Toast.makeText(Preferences.this,
getString(R.string.unable_to_change_reason,
getString(R.string.error_reason_airplane)),
Toast.LENGTH_LONG).show();
return true;
}
} catch (final SettingNotFoundException e) {
if (BuildConfig.DEBUG) Logger.log(e);
}
}
Toast.makeText(Preferences.this, R.string.unable_to_change,
Toast.LENGTH_SHORT).show();
}
} catch (SecurityException ex) {
ex.printStackTrace();
Toast.makeText(Preferences.this, "No permission to enable WiFi",
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/de/j4velin/wifiAutoOff/Receiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ private static SharedPreferences getSharedPreferences(final Context context) {
*/
@SuppressWarnings("deprecation")
private static void changeWiFi(final Context context, boolean on) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
try {
if (APILevel17Wrapper.isAirplaneModeOn(context)) {
Log.insert(context, context.getString(R.string.unable_to_change_reason,
context.getString(R.string.error_reason_airplane)), Log.Type.ERROR);
if (BuildConfig.DEBUG)
Logger.log("Unable to change WiFi state: In airplane mode on Android 8.1");
}
} catch (final SettingNotFoundException e) {
if (BuildConfig.DEBUG) Logger.log(e);
}
}
SharedPreferences prefs = getSharedPreferences(context);
if (on && prefs.getBoolean("airplane", true)) {
// check for airplane mode
Expand Down Expand Up @@ -171,7 +183,11 @@ private static void changeWiFi(final Context context, boolean on) {
if (wm.isWifiEnabled() != on) {
Log.insert(context, on ? R.string.event_turn_on : R.string.event_turn_off,
on ? Log.Type.WIFI_ON : Log.Type.WIFI_OFF);
wm.setWifiEnabled(on);
boolean success = wm.setWifiEnabled(on);
if (!success) {
Log.insert(context, R.string.unable_to_change, Log.Type.ERROR);
if (BuildConfig.DEBUG) Logger.log("Unable to change WiFi state");
}
}
} catch (Exception e) {
Toast.makeText(context, "Can not change WiFi state: " + e.getClass().getName(),
Expand Down
9 changes: 9 additions & 0 deletions src/main/res/drawable/event_error.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M11,15h2v2h-2zM11,7h2v6h-2zM11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM12,20c-4.42,0 -8,-3.58 -8,-8s3.58,-8 8,-8 8,3.58 8,8 -3.58,8 -8,8z"/>
</vector>
47 changes: 38 additions & 9 deletions src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,25 @@
<string name="turn_wifi_off_when_">Schalte WLAN aus&#8230;</string>
<string name="screen_is_off">wenn das Display aus ist</string>
<string name="not_connected_to_any_network">wenn kein Netzwerk in Reichweite</string>
<string name="sleep_policy">Die WLAN-Optionen sind momentan so eingestellt, dass das WLAN automatisch abgeschaltet wird, wenn das Display aus geht. Dieses Verhalten kann in den \'Erweiterten WLAN-Einstellungen\' von Android geändert werden.</string>
<string name="sleep_policy">Die WLAN-Optionen sind momentan so eingestellt, dass das WLAN
automatisch abgeschaltet wird, wenn das Display aus geht. Dieses Verhalten kann in den
\'Erweiterten WLAN-Einstellungen\' von Android geändert werden.
</string>
<string name="adv_wifi_settings">Erw. WLAN-Einstellungen</string>
<string name="settings_not_found_">Einstellungen nicht gefunden!</string>
<string name="minutes_before_turning_off_wifi_">Dauer in Minuten bevor WLAN ausgeschaltet werden soll:</string>
<string name="minutes_before_turning_off_wifi_">Dauer in Minuten bevor WLAN ausgeschaltet werden
soll:
</string>
<string name="turn_wifi_on_at_">Uhrzeit, zu welcher WLAN eingeschaltet werden soll:</string>
<string name="turn_wifi_off_at_">Uhrzeit, zu welcher WLAN abgeschaltet werden soll:</string>
<string name="turn_wifi_on_every">Schalte WLAN alle &#8230; ein</string>
<string name="invalid_input_number_has_to_be_">Fehlerhafte Eingabe: Zahl muss zwischen %1$d und %2$d sein</string>
<string name="invalid_input_number_has_to_be_">Fehlerhafte Eingabe: Zahl muss zwischen %1$d und
%2$d sein
</string>
<string name="power_title">beim Laden</string>
<string name="power_summary">Ignoriert während des Ladens auch die \"wenn das Display aus ist\" Einstellung</string>
<string name="power_summary">Ignoriert während des Ladens auch die \"wenn das Display aus ist\"
Einstellung
</string>

<string-array name="time_names">
<item>5 Minuten</item>
Expand All @@ -35,17 +44,28 @@

<string name="general_settings">Allgemein</string>
<string name="airplane_title">Flugmodus</string>
<string name="airplane_summary">Schalte WLAN nicht ein, wenn sich das Gerät im Flugmodus befindet</string>
<string name="airplane_summary">Schalte WLAN nicht ein, wenn sich das Gerät im Flugmodus
befindet
</string>
<string name="when_entering_location">beim Betreten dieser Orte</string>
<string name="buy_pro">In der kostenlosen Version der App wird nur ein Ort unterstützt. Möchtest du jetzt auf die Pro-Version aufwerten?</string>
<string name="buy_pro">In der kostenlosen Version der App wird nur ein Ort unterstützt. Möchtest
du jetzt auf die Pro-Version aufwerten?
</string>
<string name="location_text">Beim Betreten folgender Orte wird das WLAN eingeschaltet:</string>
<string name="location_summary">Verwende Funkmasten-Informationen um WLAN an festgelegten Orten einzuschalten</string>
<string name="timeout_too_short">\'Kein Netz in Reichweite\' Einstellung könnte für die Standort-Optionen zu kurz sein. Jetzt auf die Einstellung \'5 min\' ändern?</string>
<string name="location_summary">Verwende Funkmasten-Informationen um WLAN an festgelegten Orten
einzuschalten
</string>
<string name="timeout_too_short">\'Kein Netz in Reichweite\' Einstellung könnte für die
Standort-Optionen zu kurz sein. Jetzt auf die Einstellung \'5 min\' ändern?
</string>
<string name="no_connected">WLAN nicht verbunden</string>
<string name="click_to_select_network">Hier klicken um WLAN-Netzwerk auszuwählen</string>
<string name="wifi_not_enabled">WLAN ausgeschaltet</string>
<string name="click_to_turn_on">Hier klicken zum einzuschalten</string>
<string name="location_settings_warning">Deine Einstellungen verbieten der App deinen Standort zu bestimmen, diese Funktion wird daher nicht funktionieren. Klicke hier um die Standort-Einstellungen zu ändern</string>
<string name="location_settings_warning">Deine Einstellungen verbieten der App deinen Standort
zu bestimmen, diese Funktion wird daher nicht funktionieren. Klicke hier um die
Standort-Einstellungen zu ändern
</string>
<string name="interval">Intervall:</string>
<string name="active_location_scan">Aktive Standort-Bestimmung</string>
<string name="more_info">Mehr dazu</string>
Expand All @@ -72,4 +92,13 @@
<string name="event_no_network_canceled">"Keine Verbindung" Timer abgebrochen</string>
<string name="event_screen_off_timer">"Display aus" Timer gesetzt (%d min)</string>
<string name="event_no_network_timer">"Keine Verbindung" Timer gesetzt (%d min)</string>
<string name="delete_log">Log löschen</string>
<string name="turn_on_off">Ein-/Ausschalten</string>
<string name="hotspot_active">WiFi Hotspot aktiv, WLAN wird nicht eingeschaltet</string>
<string name="unable_to_change">WLAN Status kann nicht geändert werden</string>
<string name="unable_to_change_reason">WLAN Status kann nicht geändert werden: %s</string>
<string name="error_reason_airplane">Gerät ist im Flugmodus</string>
<string name="airplane_notice">Hinweis: In deiner Android Version kann der WLAN Status nicht
durch Apps geändert werden, wenn sich das Gerät im Flugmdus befindet!
</string>
</resources>
32 changes: 25 additions & 7 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="MissingTranslation,ExtraTranslation">
<resources xmlns:tools="http://schemas.android.com/tools"
tools:ignore="MissingTranslation,ExtraTranslation">

<string name="for_at_least">for at least %d min</string>
<string name="at_summary">everyday at %s</string>
Expand All @@ -12,16 +13,21 @@
<string name="turn_wifi_off_when_">Turn WiFi off when&#8230;</string>
<string name="screen_is_off">screen is off</string>
<string name="not_connected_to_any_network">not connected to any network</string>
<string name="sleep_policy">Your WiFi sleep policy is set to automatically disable WiFi as soon as the display goes off. You can change the policy in the Android \'Advanced WiFi settings\'</string>
<string name="sleep_policy">Your WiFi sleep policy is set to automatically disable WiFi as soon
as the display goes off. You can change the policy in the Android \'Advanced WiFi settings\'
</string>
<string name="adv_wifi_settings">Adv. WiFi settings</string>
<string name="settings_not_found_">Settings not found!</string>
<string name="minutes_before_turning_off_wifi_">Minutes before turning off WiFi:</string>
<string name="turn_wifi_on_at_">Turn WiFi on at:</string>
<string name="turn_wifi_off_at_">Turn WiFi off at:</string>
<string name="turn_wifi_on_every">Turn WiFi on every &#8230;</string>
<string name="invalid_input_number_has_to_be_">Invalid input: Number has to be >= %1$d and &lt;= %2$d</string>
<string name="invalid_input_number_has_to_be_">Invalid input: Number has to be >= %1$d and &lt;=
%2$d
</string>
<string name="power_title">when charging</string>
<string name="power_summary">Also ignores the \"when screen is off\" setting while charging</string>
<string name="power_summary">Also ignores the \"when screen is off\" setting while charging
</string>

<string-array name="time_names">
<item>5 min</item>
Expand All @@ -37,14 +43,20 @@
<string name="airplane_title">Airplane mode</string>
<string name="airplane_summary">Don\'t enable WiFi when in airplane mode</string>
<string name="when_entering_location">when entering these locations</string>
<string name="buy_pro">Only one location is support in the free version of the app. Do you want to upgrade to the pro version now?</string>
<string name="buy_pro">Only one location is support in the free version of the app. Do you want
to upgrade to the pro version now?
</string>
<string name="location_text">Turn on WiFi when entering these locations:</string>
<string name="location_summary">Use cell location information to turn WiFi on at specified locations</string>
<string name="location_summary">Use cell location information to turn WiFi on at specified
locations
</string>
<string name="no_connected">WiFi disconnected</string>
<string name="click_to_select_network">Click to select WiFi network</string>
<string name="wifi_not_enabled">WiFi disabled</string>
<string name="click_to_turn_on">Click to turn on</string>
<string name="location_settings_warning">Your settings prevent the app from detect your location</string>
<string name="location_settings_warning">Your settings prevent the app from detect your
location
</string>
<string name="interval">Interval:</string>
<string name="active_location_scan">Active location scanning</string>
<string name="more_info">More info</string>
Expand Down Expand Up @@ -73,4 +85,10 @@
<string name="delete_log">Delete log</string>
<string name="turn_on_off">Turn on/off</string>
<string name="hotspot_active">Not turning WiFi on as WiFi hotspot is active</string>
<string name="unable_to_change">Unable to change WiFi state</string>
<string name="unable_to_change_reason">Unable to change WiFi state: %s</string>
<string name="error_reason_airplane">Device is in airplane mode</string>
<string name="airplane_notice">Notice: On your Android version, apps can not change the WiFi
state while the device is in airplane mode!
</string>
</resources>
53 changes: 53 additions & 0 deletions src/main/res/xml-v26/preferences.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<de.j4velin.wifiAutoOff.NoticePreference
android:selectable="false"
android:summary="@string/airplane_notice"
android:title="@string/airplane_title"/>

<de.j4velin.wifiAutoOff.StatusPreference
android:key="status"/>

<PreferenceCategory android:title="@string/turn_wifi_on_">
<CheckBoxPreference
android:defaultValue="true"
android:key="on_unlock"
android:title="@string/when_device_gets_unlocked"/>
<CheckBoxPreference
android:defaultValue="false"
android:key="on_at"/>
<CheckBoxPreference
android:defaultValue="false"
android:key="on_every"/>
<CheckBoxPreference
android:defaultValue="false"
android:key="power_connected"
android:summary="@string/power_summary"
android:title="@string/power_title"/>
<Preference
android:key="locations"
android:summary="@string/location_summary"
android:title="@string/when_entering_location"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/turn_wifi_off_when_">
<CheckBoxPreference
android:defaultValue="true"
android:key="off_screen_off"
android:title="@string/screen_is_off"/>
<CheckBoxPreference
android:defaultValue="true"
android:key="off_no_network"
android:title="@string/not_connected_to_any_network"/>
<CheckBoxPreference
android:defaultValue="false"
android:key="off_at"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/general_settings">
<Preference
android:key="log"
android:summary="@string/log_summary"
android:title="@string/log_title"/>
</PreferenceCategory>

</PreferenceScreen>

0 comments on commit 1374ac5

Please sign in to comment.