Skip to content

Commit

Permalink
Implement auto start on boot feature.
Browse files Browse the repository at this point in the history
Can be activated via a new preference.
Closes #2.
  • Loading branch information
Vilbrekin committed Mar 22, 2014
1 parent 14316b6 commit 3406539
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 9 deletions.
13 changes: 10 additions & 3 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.poirsouille.tinc_gui"
android:versionCode="7"
android:versionName="0.9.6" >
android:versionCode="8"
android:versionName="0.9.7" >

<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
Expand All @@ -25,6 +26,12 @@
<activity android:name=".SettingsActivity"></activity>
<activity android:name=".SettingsActivityOld"></activity>
<activity android:name=".AboutActivity"></activity>

<receiver android:name=".BootReceiver" android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
</application>

</manifest>
1 change: 1 addition & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ making a donation</a> if you like this program.
<string name="about">About</string>
<string name="pref_title_debug_level">Debug level</string>
<string name="pref_title_super_user">Execute as Super User</string>
<string name="pref_title_autostart_boot">Auto start on boot</string>

</resources>
1 change: 1 addition & 0 deletions res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
android:title="@string/max_log_size" />
<ListPreference android:defaultValue="2" android:key="pref_key_debug_level" android:title="@string/pref_title_debug_level" android:entries="@array/debug_lvl_entries" android:entryValues="@array/debug_lvl_values"/>
<CheckBoxPreference android:defaultValue="true" android:key="pref_key_super_user" android:title="@string/pref_title_super_user"/>
<CheckBoxPreference android:defaultValue="false" android:key="pref_key_autostart_boot" android:title="@string/pref_title_autostart_boot"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/pref_title_current_config" android:key="pref_key_config">
</PreferenceCategory><PreferenceCategory android:title="@string/pref_title_hosts" android:key="pref_key_hosts">
Expand Down
32 changes: 32 additions & 0 deletions src/org/poirsouille/tinc_gui/BootReceiver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.poirsouille.tinc_gui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

public class BootReceiver extends BroadcastReceiver
{

@Override
public void onReceive(Context iContext, Intent iIntent)
{
if (iIntent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
// Auto start service if enabled
Log.d(Tools.TAG, "Boot notif");
SharedPreferences aSharedPref = iContext.getSharedPreferences("org.poirsouille.tinc_gui_preferences", Context.MODE_PRIVATE);
Boolean aAutoStart = false;
aAutoStart = aSharedPref.getBoolean("pref_key_autostart_boot", aAutoStart);
if (aAutoStart)
{
Log.i(Tools.TAG, "Autostarting Tinc GUI service");
// Start tincd service
Intent intent = new Intent(iContext, TincdService.class);
iContext.startService(intent);
}
}
}

}
26 changes: 20 additions & 6 deletions src/org/poirsouille/tinc_gui/TincdService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.zip.CRC32;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.pm.PackageManager;
import android.os.Binder;
import android.os.IBinder;
import android.preference.PreferenceManager;
Expand Down Expand Up @@ -290,26 +293,37 @@ public void onCreate()
// (see http://stackoverflow.com/questions/2542938/sharedpreferences-onsharedpreferencechangelistener-not-being-called-consistently)
_prefChangeListener = new OnSharedPreferenceChangeListener()
{
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String iKey)
{
refreshPrefs();
refreshPrefs(iKey);
}
};
_sharedPref.registerOnSharedPreferenceChangeListener(_prefChangeListener);
// Refresh at startup as well
refreshPrefs();
refreshPrefs("");
}

/**
* Refresh member variables from preferences screen.
*/
private void refreshPrefs()
private void refreshPrefs(String iKey)
{
Log.d(Tools.TAG, "Refreshing preferences");
Log.d(Tools.TAG, "Refreshing preferences for key " + iKey);
_configPath = _sharedPref.getString("pref_key_config_path", _configPath);
_maxLogSize = Integer.parseInt(_sharedPref.getString("pref_key_max_log_size", "" + _maxLogSize));
_debugLvl = Integer.parseInt(_sharedPref.getString("pref_key_debug_level", "" + _debugLvl));
_useSU = _sharedPref.getBoolean("pref_key_super_user", _useSU);

if (iKey.equals("pref_key_autostart_boot"))
{
// Enable/disable boot time notification
Boolean aAutoStart = false;
aAutoStart = _sharedPref.getBoolean("pref_key_autostart_boot", aAutoStart);
this.getPackageManager().setComponentEnabledSetting(new ComponentName(this, BootReceiver.class),
aAutoStart ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Log.d(Tools.TAG, "Changing boot status notification state: " + aAutoStart);
}
}

public void onDestroy ()
Expand Down Expand Up @@ -365,7 +379,7 @@ public List<String> popOutput()
public void call(String iData)
{
Date aDate = new Date();
SimpleDateFormat aFormat = new SimpleDateFormat("HH:mm:ss");
SimpleDateFormat aFormat = new SimpleDateFormat("HH:mm:ss", Locale.US);
String aTxt = aFormat.format(aDate) + " " + iData;
// Notify activity callback if any
if (_callback != null)
Expand Down

0 comments on commit 3406539

Please sign in to comment.