From e1e8c224872bc442e3f55ce6011eaa87f45077de Mon Sep 17 00:00:00 2001 From: amoghbl1 Date: Mon, 16 Dec 2013 04:31:49 +0530 Subject: [PATCH 1/5] notification updating bug fix(Prevent service from being killed) --- project.properties | 2 +- src/org/torproject/android/service/TorService.java | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/project.properties b/project.properties index 0629393..0a52332 100644 --- a/project.properties +++ b/project.properties @@ -8,5 +8,5 @@ # project structure. # Project target. -target=android-17 +target=android-18 android.library.reference.1=external/ActionBarSherlock/actionbarsherlock diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java index 69b46fd..4fd976a 100644 --- a/src/org/torproject/android/service/TorService.java +++ b/src/org/torproject/android/service/TorService.java @@ -100,6 +100,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst private long lastRead = -1; private long lastWritten = -1; + private static int notificationCounter = 0; private NotificationManager mNotificationManager = null; @@ -1180,6 +1181,9 @@ public class TorService extends Service implements TorServiceConstants, TorConst mTotalTrafficRead += read; sendCallbackStatusMessage(written, read, mTotalTrafficWritten, mTotalTrafficRead); + + if(++notificationCounter%10==0) + startService(new Intent(INTENT_TOR_SERVICE)); } lastWritten = written; -- 1.8.3.2 From 6affc33866b95279cab09fa0d1a5e06b85dd1638 Mon Sep 17 00:00:00 2001 From: amoghbl1 Date: Mon, 16 Dec 2013 04:45:13 +0530 Subject: [PATCH 2/5] stop button image added to res/drawable (for the notification button) --- .settings/org.eclipse.jdt.core.prefs | 4 ++++ project.properties | 2 +- res/drawable/button_stop.png | Bin 0 -> 1462 bytes 3 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 res/drawable/button_stop.png diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..b080d2d --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/project.properties b/project.properties index 0a52332..0629393 100644 --- a/project.properties +++ b/project.properties @@ -8,5 +8,5 @@ # project structure. # Project target. -target=android-18 +target=android-17 android.library.reference.1=external/ActionBarSherlock/actionbarsherlock diff --git a/res/drawable/button_stop.png b/res/drawable/button_stop.png new file mode 100644 index 0000000000000000000000000000000000000000..d97a3a8edc3806086df0b9c54e3d3233ed72918c GIT binary patch literal 1462 zcmV;n1xfmeP)+|D3;>~N|IV|7zk>*z-JP;KaRLOy2@nt`KtP-T0dWEZ{3YNqYi_-56j1=4U9Y|C z?s|O-#sMNHjve2UA~jrI5K!?{cmUyCg#hITz+dP){DQppfi|HHLV~s+(Y%y5-l!8_ zOVfg=6t%UzyK!vCyPWYlqpamYD74j_WVIV>Z{IgFXU-&9S;?PUUS0TdX*K`B($bRm z{{rB9EUqkGSX{|}f$z;hFew0FqpP1RJ`g1FRyaKNm3+o`nHSJq0C?l{GC!W>1+FB^ zXEw&d;jaacw7tB#_-Fpp{5Vqr0Mg?U&`*&_G@8p~{Sy*!ve9n5%Z>nxd%^1^!`<=1KGla(sWe3b z9X^>#s2~tSJ2o7WBysS}C`&n@aoA~F$MC}vRFIHKXFvkykU)b^rITtf5F81Q_lJjv zCTDVWW(0hDW$Svo(|*vXH!$#M34Rr2vsog62A|HP)L<|~g|9d2sNL@TwYhoidu9Y! z5Jx@p^LzLAG)>p368s=xHkTt3hT${0v>FQ0;dR46_wMa!M^^7M3xcU>@apBOznNY1 zezjWtcL@PMnwy&=68i8iVLLaQg@hNT!-Lpzxvb%5;Ab&e2yJd|-o_H}i)u}cDj@(8 zUOjuZF9E~P&gIl_m=3RL8Y-4beR%u~mMcLbp;#*G&4z|(i7^-oqWSrGW&Yebbu2uF zc%B~#-)uC|?ruqUz_ToXNI0@C>=ui9v+0oFIL0{v4+#v9V;H^x@H@Lj9p8(1119j+-9b{z1G3!qCVfQ0&!`tgiG_Cp)_yk4)P!kq#Io?!v>oB$FE zI|fc>rfH&1r-O?bn9tj&+wD3e=y1-R-wQ^sOW>br2X`pQwk-lS!!S^(ST^v7sZ>%? zd_Dp|&KpFt*~Ai9FNF#Gjj8D=*#|(w)g8~<#|H!Wp4D0CLCY3P707!!7DIBvNg0Zw zphP0!Oc^h7VZf{R_mPPh<*h|{t0DkkEt((Wi5(v??rsk*#;97FM zBFktxKHZl9&w7oiApou7g%_(%#$qq|{eB|Fxd5`^E9DBncl+?ey$5IgnIs+#@^H22 zD3SN}?*HLe0;%=&^)3@VfaA=!y^>cRf_FgJ>-iJ#l(HEoyy2d3G6EpjD@ozUjmIA` zGXzYIn-nEcqzQtS2l%RC4#CqTRBBbNIgl_aM9JfVou3&2yk(Z5%iG<&?IFtsp_a(Rd^QB9djHhtV524TdIFow>E&t=i zix Date: Mon, 16 Dec 2013 04:55:32 +0530 Subject: [PATCH 3/5] the onNewIntent() in Orbot.java was modified to handle the stop button being clicked. --- src/org/torproject/android/Orbot.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/org/torproject/android/Orbot.java b/src/org/torproject/android/Orbot.java index 670de66..30fb867 100644 --- a/src/org/torproject/android/Orbot.java +++ b/src/org/torproject/android/Orbot.java @@ -438,6 +438,24 @@ public class Orbot extends SherlockActivity implements TorConstants, OnLongClick super.onNewIntent(intent); + //Handle the intent which launches this class when the stop button (in notification) is clicked + int stopTorFlag = intent.getIntExtra("stopTorFlag",0); + //If the stop tor flag was set to 1 then we need to stop the application , by observing the previous code we see that we + //need to call stopTor(). + if(stopTorFlag==1) + { + try { + stopTor(); + } catch (RemoteException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + //We also kill the android application process so that the user can return to the application he was on when he clicked the + //stop button. + + android.os.Process.killProcess(android.os.Process.myPid()); + } + updateStatus(""); handleIntents(); } -- 1.8.3.2 From 8d2244b9663bb372f04f6ee91199532c7af409ef Mon Sep 17 00:00:00 2001 From: amoghbl1 Date: Mon, 16 Dec 2013 05:14:48 +0530 Subject: [PATCH 4/5] showToolbarNotification() has been updated to create the notification in a newer manner(with respect to new api) --- src/org/torproject/android/service/TorService.java | 59 ++++++++++++++-------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java index 4fd976a..a71f014 100644 --- a/src/org/torproject/android/service/TorService.java +++ b/src/org/torproject/android/service/TorService.java @@ -33,6 +33,7 @@ import org.torproject.android.settings.AppManager; import org.torproject.android.share.ShareItem; import org.torproject.android.share.ShareService; +import android.annotation.SuppressLint; import android.app.Application; import android.app.Notification; import android.app.NotificationManager; @@ -202,31 +203,49 @@ public class TorService extends Service implements TorServiceConstants, TorConst } + @SuppressLint("NewApi") private void showToolbarNotification (String notifyMsg, int notifyId, int icon, int flags) { - - - CharSequence tickerText = notifyMsg; - long when = System.currentTimeMillis(); - - Notification notification = new Notification(icon, tickerText, when); - - if (prefPersistNotifications && flags != -1) - notification.flags |= flags; - - Context context = getApplicationContext(); - CharSequence contentTitle = getString(R.string.app_name); - CharSequence contentText = notifyMsg; - + //Reusable code has been moved here. Intent notificationIntent = new Intent(this, Orbot.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); - - notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); - - mNotificationManager.notify(notifyId, notification); - - + + //The code which was originally present here has been modified in order to implement newer standards keeping in mind the + //problem of backward compatibility. + if(Build.VERSION.SDK_INT=Build.VERSION_CODES.JELLY_BEAN) + { + Notification notification = new Notification.Builder(this) + .setSmallIcon(icon) + .setContentTitle(getString(R.string.app_name)) + .setContentText(notifyMsg) + .setContentIntent(contentIntent) + .build(); + + if (prefPersistNotifications && flags != -1) + notification.flags |= flags; + + mNotificationManager.notify(notifyId,notification); + } } /* (non-Javadoc) -- 1.8.3.2 From f47305b450d5de4f24b9505eb99aa57edece9c43 Mon Sep 17 00:00:00 2001 From: amoghbl1 Date: Mon, 16 Dec 2013 05:59:50 +0530 Subject: [PATCH 5/5] final changes made to the notification service and orbot.java in order to make the stop button fully functional --- src/org/torproject/android/Orbot.java | 9 +-- src/org/torproject/android/service/TorService.java | 67 +++++++++++++++------- 2 files changed, 51 insertions(+), 25 deletions(-) diff --git a/src/org/torproject/android/Orbot.java b/src/org/torproject/android/Orbot.java index 30fb867..fb096cc 100644 --- a/src/org/torproject/android/Orbot.java +++ b/src/org/torproject/android/Orbot.java @@ -34,7 +34,6 @@ import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.os.RemoteException; -import android.preference.PreferenceManager; import android.text.ClipboardManager; import android.text.Layout; import android.text.method.ScrollingMovementMethod; @@ -47,7 +46,6 @@ import android.view.View; import android.view.View.OnLongClickListener; import android.view.View.OnTouchListener; import android.view.animation.AccelerateInterpolator; -import android.view.animation.Animation; import android.widget.Button; import android.widget.SlidingDrawer; import android.widget.TextView; @@ -442,6 +440,7 @@ public class Orbot extends SherlockActivity implements TorConstants, OnLongClick int stopTorFlag = intent.getIntExtra("stopTorFlag",0); //If the stop tor flag was set to 1 then we need to stop the application , by observing the previous code we see that we //need to call stopTor(). + Log.i(TAG,"stopTorFlag"+stopTorFlag); if(stopTorFlag==1) { try { @@ -452,8 +451,10 @@ public class Orbot extends SherlockActivity implements TorConstants, OnLongClick } //We also kill the android application process so that the user can return to the application he was on when he clicked the //stop button. - - android.os.Process.killProcess(android.os.Process.myPid()); + //Luckily for me , the unbindService() exitst! If we just try to do this.finish() , we get a service has leaked error + //in the logcat and therefore I have used unbindService() before it and it seems to work perfectly! + unbindService(); + this.finish(); } updateStatus(""); diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java index a71f014..194bc46 100644 --- a/src/org/torproject/android/service/TorService.java +++ b/src/org/torproject/android/service/TorService.java @@ -1076,37 +1076,62 @@ public class TorService extends Service implements TorServiceConstants, TorConst } NotificationCompat.Builder mNotifyBuilder; + Notification.Builder mNewNotifyBuilder; + @SuppressLint("NewApi") private void startNotification (String message, boolean persistent) { + //Reusable code. + Intent intent = new Intent(TorService.this, Orbot.class); + PendingIntent pendIntent = PendingIntent.getActivity(TorService.this, 0, intent, 0); - if (mNotifyBuilder == null) + if (mNotificationManager == null) + mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + + //Older version of code is here and will be used if api < 16. + if(Build.VERSION.SDK_INT=Build.VERSION_CODES.JELLY_BEAN) + { + if(mNewNotifyBuilder==null) + { + Intent stopTor = new Intent(this,Orbot.class); + stopTor.putExtra("stopTorFlag",1); + PendingIntent stopTorIntent = PendingIntent.getActivity(this, 0, stopTor, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_UPDATE_CURRENT); + + mNewNotifyBuilder = new Notification.Builder(this) + .setContentTitle(getString(R.string.app_name)) + .setContentText(getString(R.string.status_activated)) + .setSmallIcon(R.drawable.ic_stat_tor) + .addAction(R.drawable.button_stop, "STOP", stopTorIntent); + + mNewNotifyBuilder.setContentIntent(pendIntent); + } + mNewNotifyBuilder.setOngoing(persistent); + mNewNotifyBuilder.setContentText(message); - } - - if (mNotificationManager == null) - { - mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - + mNotificationManager.notify( + NOTIFY_ID, + mNewNotifyBuilder.build()); } - - mNotifyBuilder.setOngoing(persistent); - mNotifyBuilder.setContentText(message); - - mNotificationManager.notify( - NOTIFY_ID, - mNotifyBuilder.getNotification()); } -- 1.8.3.2