Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

bootreceiver & networkreceiver. before service refactor

  • Loading branch information...
commit af164bfaf1f4c9a8c252c1065e4bb5c8ff46164b 1 parent cd31ca3
mb authored
View
11 AndroidManifest.xml
@@ -6,6 +6,7 @@
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" ></uses-permission>
+ <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".TasksActivity"
android:label="@string/app_name">
@@ -16,6 +17,16 @@
</activity>
<activity android:name=".PreferencesActivity" android:label="@string/preferences_menu_title"></activity>
<service android:name=".SynchronisationService"></service>
+ <receiver android:name=".NetworkReceiver">
+ <intent-filter >
+ <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
+ </intent-filter>
+ </receiver>
+ <receiver android:name=".BootReceiver">
+ <intent-filter>
+ <action android:name="android.intent.action.BOOT_COMPLETED" />
+ </intent-filter>
+ </receiver>
</application>
</manifest>
View
2  res/values/arrays.xml
@@ -7,6 +7,7 @@
<item>3</item>
<item>4</item>
<item>5</item>
+ <item>6</item>
</string-array>
<string-array name="sync_time_array_entries">
@@ -16,6 +17,7 @@
<item>"co 30 minut"</item>
<item>"co 60 minut"</item>
<item>"Wyłączona"</item>
+ <item>"test: 10s"</item>
</string-array>
</resources>
View
1  res/values/strings.xml
@@ -23,4 +23,5 @@
<string name="sync_status_bar_txt_error">Synchronizacja: Wystąpił błąd</string>
<string name="sync_notification_body_err">Nieprawidłowe dane użytkownika</string>
<string name="sync_notification_body_ser_err">Nie odnaleziono serwera</string>
+ <string name="dialog_text_no_network">Brak połączenia z Internetem</string>
</resources>
View
18 src/org/supervisor/BaseActivity.java
@@ -1,7 +1,10 @@
package org.supervisor;
import android.app.Activity;
+import android.content.Context;
import android.content.Intent;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
@@ -11,7 +14,6 @@
public class BaseActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
}
@@ -26,18 +28,22 @@ public boolean onCreateOptionsMenu(Menu menu) {
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.synchronise:
- startService(new Intent(this, SynchronisationService.class));
- //ProgressDialog dialog = ProgressDialog.show(BaseActivity.this, "",
- //getString(R.string.dialog_text), true);
- Toast.makeText(this, R.string.dialog_text, Toast.LENGTH_SHORT).show();
+ NetworkInfo networkInfo = ((ConnectivityManager) getApplicationContext()
+ .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
+ if ( !(networkInfo == null || !networkInfo.isConnected()) ) {
+ startService(new Intent(this, SynchronisationService.class));
+ }
+ else
+ Toast.makeText(this, R.string.dialog_text_no_network, Toast.LENGTH_SHORT).show();
break;
case R.id.tasklist:
startActivity(new Intent(this, TasksActivity.class));
- break;
+ break;
case R.id.preferences:
startActivity(new Intent(this, PreferencesActivity.class));
+ break;
}
return true;
}
View
18 src/org/supervisor/BootReceiver.java
@@ -0,0 +1,18 @@
+package org.supervisor;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+
+import android.util.Log;
+
+public class BootReceiver extends BroadcastReceiver {
+
+ @Override
+ public void onReceive(Context context, Intent intent) {
+
+
+ Log.d("bootreceiver", "boot receiver started service");
+ context.startService(new Intent(context, SynchronisationService.class));
+ }
+}
View
29 src/org/supervisor/NetworkReceiver.java
@@ -0,0 +1,29 @@
+package org.supervisor;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
+import android.util.Log;
+
+public class NetworkReceiver extends BroadcastReceiver {
+
+ public void onReceive(Context context, Intent intent) {
+
+ NetworkInfo networkInfo = ((ConnectivityManager) context
+ .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
+ boolean connected = !(networkInfo == null || !networkInfo.isConnected());
+
+ if(connected) {
+ Log.d("netreceiver", "net receiver started service");
+ context.startService(new Intent(context, SynchronisationService.class));
+ }
+ else {
+ Log.d("netreceiver", "net receiver stopped service");
+ context.stopService(new Intent(context, SynchronisationService.class));
+ }
+
+ }
+
+}
View
12 src/org/supervisor/PreferencesActivity.java
@@ -1,12 +1,12 @@
package org.supervisor;
+import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
-import android.util.Log;
public class PreferencesActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener{
@@ -16,16 +16,17 @@
public static final String server_pref_key = "server";
public static final String username_pref_key = "username";
public static final String password_pref_key = "password";
+ public static final String TAG = PreferencesActivity.class.getSimpleName();
+ private boolean init;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
-
+ savedInstanceState.getBoolean( --here check if its init (first app run))
addPreferencesFromResource(R.xml.preferences);
lpref = (ListPreference)findPreference(sync_pref_key);
lpref.setSummary(lpref.getEntry());
pref = (EditTextPreference) findPreference(server_pref_key);
- Log.d("test", pref.toString());
if (pref.getText() != null && !pref.getText().trim().equals(""))
pref.setSummary(pref.getText());
@@ -57,6 +58,10 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
if (key.equals(sync_pref_key)) {
lpref = (ListPreference) findPreference(key);
lpref.setSummary(lpref.getEntry());
+ if(!lpref.getValue().equals("5"))
+ startService(new Intent(this, SynchronisationService.class));
+ else
+ stopService(new Intent(this, SynchronisationService.class));
}
else if (key.equals(server_pref_key)) {
@@ -76,7 +81,6 @@ else if (key.equals(password_pref_key) ) {
if(!pref.getText().trim().equals(""))
pref.setSummary(null);
}
-
}
View
92 src/org/supervisor/SynchronisationService.java
@@ -13,6 +13,8 @@
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
@@ -24,31 +26,40 @@
private static final String TAG = SynchronisationService.class.getSimpleName();
private SynchronisationThread thread;
- private long SYNC_DELAY = 360000; //6min
private long NOTIFICATION_CANCEL_DELAY = 1000; //1s after GET
private int NOTIFICATION_ID = 1;
private NotificationManager mgr;
private DataStorage dataStorage;
private SharedPreferences prefs;
+ private String status_text;
+ private String text;
+ private String title;
public IBinder onBind(Intent intent) {
return null;
}
+
public void onCreate(){
super.onCreate();
thread = new SynchronisationThread();
Log.d(TAG, "onCreate() called");
dataStorage = new DataStorage(this);
+ status_text = getString(R.string.sync_status_bar_txt);
+ text = getString(R.string.sync_notification_body);
+ title = getString(R.string.sync_notification_title);
}
+
public void onDestroy() {
super.onDestroy();
thread.interrupt();
thread = null;
- Log.d(TAG, "onDestroy() called");
+ dataStorage.close();
+ Log.d(TAG, "SERVICE STOPPED : onDestroy() called");
}
+
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
if(thread.isAlive()) { //forced synchronisation
@@ -56,10 +67,17 @@ public int onStartCommand(Intent intent, int flags, int startId) {
thread = new SynchronisationThread();
}
thread.start();
- Log.d(TAG, "onStartCommand() called");
+ Log.d(TAG, "SERVICE STARTED : onStartCommand() called");
return START_STICKY;
}
+
+ private boolean isNetworkOn(Context context) {
+ NetworkInfo networkInfo = ((ConnectivityManager) context
+ .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
+ return !(networkInfo == null || !networkInfo.isConnected());
+ }
+
private class SynchronisationThread extends Thread {
@@ -68,12 +86,41 @@ public int onStartCommand(Intent intent, int flags, int startId) {
public void run() {
Log.d(TAG, "run");
try{
- while(true){
+ boolean keep_alive = true;
+ do{
+ prefs = PreferenceManager.getDefaultSharedPreferences(SynchronisationService.this);
+ Long sync_delay = null;
+ int sync_option = Integer.parseInt(prefs.getString(PreferencesActivity.sync_pref_key, "4"));
+
+ Log.d(TAG, Integer.toString(sync_option));
+ switch (sync_option) {
+ case 0:
+ sync_delay = new Long(300000);
+ break;
+ case 1:
+ sync_delay = new Long(600000);
+ break;
+ case 2:
+ sync_delay = new Long(900000);
+ break;
+ case 3:
+ sync_delay = new Long(1800000);
+ break;
+ case 4:
+ sync_delay = new Long(3600000);
+ break;
+ case 5:
+ keep_alive = false;
+ break;
+ case 6:
+ sync_delay = new Long(10000);
+ break;
- prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
+ }
+ if( isNetworkOn(SynchronisationService.this)) {
+ Log.d(TAG, "Network on!");
String username = prefs.getString(PreferencesActivity.username_pref_key, null);
String password = prefs.getString(PreferencesActivity.password_pref_key, null);
- URL server = null;
ApiManager.setCredentials(username, password);
String adress = "http://" + prefs.getString(PreferencesActivity.server_pref_key, null) +"/";
try {
@@ -82,17 +129,11 @@ public void run() {
Log.d(TAG, adress);
Log.d(TAG, e.getMessage());
}
- Log.d(TAG,adress);
ApiManager.HOST = adress;
- //http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/RemoteService.html
-
String ns = Context.NOTIFICATION_SERVICE;
mgr = (NotificationManager) getSystemService(ns);
int icon = R.drawable.ic_menu_refresh;
- String status_text = getString(R.string.sync_status_bar_txt);
- String text = getString(R.string.sync_notification_body);
- String title = getString(R.string.sync_notification_title);
ArrayList<Task> tasks = null;
Intent intent = null;
boolean request_ok = true;
@@ -106,7 +147,7 @@ public void run() {
} catch (NetworkErrorException e) {
Log.d(TAG, e.getMessage());
- intent = new Intent(getApplicationContext(), PreferencesActivity.class);
+ intent = new Intent(SynchronisationService.this, PreferencesActivity.class);
status_text = getString(R.string.sync_status_bar_txt_error);
if (e.getMessage().equals("404"))
text = getString(R.string.sync_notification_body_ser_err);
@@ -114,14 +155,14 @@ public void run() {
text = getString(R.string.sync_notification_body_err);
request_ok = false;
}
-
+
Notification not = new Notification(icon, status_text, System.currentTimeMillis());
not.flags |= Notification.FLAG_AUTO_CANCEL;
- PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
- not.setLatestEventInfo(getApplicationContext(), title,
+ PendingIntent pi = PendingIntent.getActivity(SynchronisationService.this, 0, intent, 0);
+ not.setLatestEventInfo(SynchronisationService.this, title,
text, pi);
mgr.notify(NOTIFICATION_ID, not);
-
+
ContentValues values = new ContentValues();
if (tasks != null)
for (Task task : tasks) {
@@ -141,14 +182,23 @@ public void run() {
dataStorage.insert(values);
}
dataStorage.close();
-
+
if(request_ok) {
sleep(NOTIFICATION_CANCEL_DELAY);
mgr.cancel(NOTIFICATION_ID);
}
- sleep(SYNC_DELAY);
- }
- } catch (InterruptedException e) {Log.d(TAG, "interrupted exception"); }
+ } // network available condition ends here
+
+ if(keep_alive) //wait for set amount of time and run thread again
+ sleep(sync_delay);
+ //check again if keep_alive didn't change
+ keep_alive = (prefs.getString(PreferencesActivity.sync_pref_key, "4").equals("5")) ? false : true;
+ Log.d(TAG, Boolean.toString(keep_alive));
+ } while (keep_alive);
+ SynchronisationService.this.stopSelf(); //stop service if it was forced (1-time) sync
+ } catch (InterruptedException e) {
+ Log.d(TAG, "interrupted exception");
+ }
}
}
View
20 src/org/supervisor/TasksActivity.java
@@ -1,7 +1,10 @@
package org.supervisor;
+import android.content.Intent;
+import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
+import android.preference.PreferenceManager;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
@@ -18,7 +21,22 @@
public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
+ super.onCreate(savedInstanceState);
+ /*
+ * this is a main activity, after user launches it for first time
+ * (we assume that first time is when properties are empty)
+ * we launch preferences activity -
+ * which after user fills server adress - starts the sync service
+ */
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
+ String server = prefs.getString(PreferencesActivity.server_pref_key, null);
+ String username = prefs.getString(PreferencesActivity.username_pref_key, null);
+ String password = prefs.getString(PreferencesActivity.password_pref_key, null);
+ if(server == null || username == null || password == null) {
+ Intent intent = new Intent(this, PreferencesActivity.class).putE;
+ startActivity(intent);
+ }
+
setContentView(R.layout.tasklist);
taskList = (ListView) findViewById(R.id.tasklist);
}
Please sign in to comment.
Something went wrong with that request. Please try again.