Skip to content

Commit

Permalink
Improved power trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
meefik committed Oct 20, 2019
1 parent 8fa920f commit 5190233
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [2.4.1] - 2019-10-20
### Added
- Added power trigger

## [2.4.0] - 2019-08-12
### Added
- Added support Alpine Linux
Expand Down
6 changes: 3 additions & 3 deletions app/build.gradle
Expand Up @@ -8,8 +8,8 @@ android {
applicationId 'ru.meefik.linuxdeploy'
minSdkVersion 15
targetSdkVersion 28
versionCode 251
versionName "2.4.0"
versionCode 252
versionName "2.4.1"
}
buildTypes {
release {
Expand All @@ -29,7 +29,7 @@ android {

dependencies {
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.browser:browser:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
}
21 changes: 9 additions & 12 deletions app/src/main/AndroidManifest.xml
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="ru.meefik.linuxdeploy"
android:installLocation="internalOnly">

Expand All @@ -13,12 +14,12 @@

<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/DarkTheme"
android:usesCleartextTraffic="true">
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
Expand Down Expand Up @@ -92,15 +93,6 @@
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
</intent-filter>
</receiver>
<receiver
android:name="PowerReceiver"
android:enabled="false"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
</intent-filter>
</receiver>
<receiver
android:name=".ActionReceiver"
android:enabled="true"
Expand All @@ -109,9 +101,14 @@
<action android:name="ru.meefik.linuxdeploy.BROADCAST_ACTION" />
</intent-filter>
</receiver>
<receiver
android:name=".PowerReceiver"
android:enabled="false"
android:exported="false">
</receiver>
<receiver
android:name=".NetworkReceiver"
android:enabled="true"
android:enabled="false"
android:exported="false">
</receiver>

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/assets/env
36 changes: 27 additions & 9 deletions app/src/main/java/ru/meefik/linuxdeploy/MainActivity.java
Expand Up @@ -47,6 +47,7 @@ public class MainActivity extends AppCompatActivity implements
private static PowerManager.WakeLock wakeLock;

private NetworkReceiver networkReceiver;
private PowerReceiver powerReceiver;

private NetworkReceiver getNetworkReceiver() {
if (networkReceiver == null)
Expand All @@ -55,6 +56,13 @@ private NetworkReceiver getNetworkReceiver() {
return networkReceiver;
}

private PowerReceiver getPowerReceiver() {
if (powerReceiver == null)
powerReceiver = new PowerReceiver();

return powerReceiver;
}

/**
* Show message in TextView, used from Logger
*
Expand All @@ -80,7 +88,9 @@ public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}

DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
Expand Down Expand Up @@ -113,6 +123,16 @@ public void onCreate(Bundle savedInstanceState) {
unregisterReceiver(networkReceiver);
}

// Power receiver
if (PrefStore.isPowerTrack(this)) {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(getPowerReceiver(), filter);
} else if (powerReceiver != null) {
unregisterReceiver(powerReceiver);
}

if (EnvUtils.isLatestVersion(this)) {
// start services
EnvUtils.execServices(getBaseContext(), new String[]{"telnetd", "httpd"}, "start");
Expand Down Expand Up @@ -269,7 +289,7 @@ public void onResume() {

// Wake lock
if (PrefStore.isWakeLock(this)) {
if (!wakeLock.isHeld()) wakeLock.acquire();
if (!wakeLock.isHeld()) wakeLock.acquire(60*60*1000L /*60 minutes*/);
} else {
if (wakeLock.isHeld()) wakeLock.release();
}
Expand Down Expand Up @@ -420,13 +440,11 @@ private void updateEnvWithRequestPermissions() {
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_WRITE_STORAGE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
new UpdateEnvTask(this).execute();
} else {
Toast.makeText(this, getString(R.string.write_permissions_disallow), Toast.LENGTH_LONG).show();
}
if (requestCode == REQUEST_WRITE_STORAGE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
new UpdateEnvTask(this).execute();
} else {
Toast.makeText(this, getString(R.string.write_permissions_disallow), Toast.LENGTH_LONG).show();
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/ru/meefik/linuxdeploy/PowerReceiver.java
Expand Up @@ -8,6 +8,10 @@ public class PowerReceiver extends BroadcastReceiver {

@Override
public void onReceive(final Context context, Intent intent) {
EnvUtils.execService(context, "start", "core/power");
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
EnvUtils.execService(context, "stop", "core/power");
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
EnvUtils.execService(context, "start", "core/power");
}
}
}
10 changes: 10 additions & 0 deletions app/src/main/java/ru/meefik/linuxdeploy/PrefStore.java
Expand Up @@ -371,6 +371,16 @@ static Boolean isNetTrack(Context c) {
return SETTINGS.get(c, "nettrack").equals("true");
}

/**
* Track changes of the power status is enabled
*
* @param c context
* @return true if enabled
*/
static Boolean isPowerTrack(Context c) {
return SETTINGS.get(c, "powertrack").equals("true");
}

/**
* Show icon is enabled
*
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values-ru/strings.xml
Expand Up @@ -247,5 +247,9 @@
<string name="hint_mounts_target">Целевая (опционально)</string>
<string name="dialog_title_net_trigger_preference">Путь к скрипту триггера</string>
<string name="title_net_trigger_preference">Сетевой триггер</string>
<string name="dialog_title_power_trigger_preference">Путь к скрипту триггера</string>
<string name="title_power_trigger_preference">Триггер питания</string>
<string name="summary_powertrack_preference">Отслеживать изменения питания и обновлять контейнер</string>
<string name="title_powertrack_preference">Отслеживать изменения питания</string>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/preferences.xml
Expand Up @@ -13,6 +13,7 @@
<string name="autostart" translatable="false">false</string>
<string name="autostart_delay" translatable="false">30</string>
<string name="nettrack" translatable="false">false</string>
<string name="powertrack" translatable="false">false</string>
<string name="env_dir" translatable="false"></string>
<string name="repository_url" translatable="false">http://hub.meefik.ru</string>
<string name="path" translatable="false"></string>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -103,6 +103,9 @@
<!-- Network trigger -->
<string name="title_nettrack_preference">Track network changes</string>
<string name="summary_nettrack_preference">Track changes of the network and update the container</string>
<!-- Power trigger -->
<string name="title_powertrack_preference">Track power changes</string>
<string name="summary_powertrack_preference">Track changes of the power and update the container</string>

<!-- ENVIRONMENT -->
<string name="env_preferences">Environment</string>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/xml/settings.xml
Expand Up @@ -85,6 +85,11 @@
android:key="nettrack"
android:summary="@string/summary_nettrack_preference"
android:title="@string/title_nettrack_preference" />
<CheckBoxPreference
android:defaultValue="@string/powertrack"
android:key="powertrack"
android:summary="@string/summary_powertrack_preference"
android:title="@string/title_powertrack_preference" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/env_preferences">
<EditTextPreference
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -6,7 +6,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath 'com.android.tools.build:gradle:3.5.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit 5190233

Please sign in to comment.