Notification fix and change in finding existing process

- Only build expanded notification on supported platforms
- If service is re-created() try to find existing process then, and don't wait for bind() from client
This commit is contained in:
Nathan Freitas 2014-08-07 11:54:31 -04:00
parent 387e74ec7c
commit c517d0fcee
1 changed files with 93 additions and 82 deletions

View File

@ -237,6 +237,42 @@ public class TorService extends Service implements TorServiceConstants, TorConst
Intent intent = new Intent(TorService.this, Orbot.class);
PendingIntent pendIntent = PendingIntent.getActivity(TorService.this, 0, intent, 0);
if (mNotifyBuilder == null)
{
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (mNotifyBuilder == null)
{
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.app_name))
.setSmallIcon(R.drawable.ic_stat_tor);
mNotifyBuilder.setContentIntent(pendIntent);
}
}
mNotifyBuilder.setContentText(notifyMsg);
mNotifyBuilder.setSmallIcon(icon);
if (notifyType != NOTIFY_ID)
{
mNotifyBuilder.setTicker(notifyMsg);
// mNotifyBuilder.setLights(Color.GREEN, 1000, 1000);
}
else
{
mNotifyBuilder.setTicker(null);
}
mNotifyBuilder.setOngoing(prefPersistNotifications);
mNotification = mNotifyBuilder.build();
if (Build.VERSION.SDK_INT >= 16) {
// Create remote view that needs to be set as bigContentView for the notification.
RemoteViews expandedView = new RemoteViews(this.getPackageName(),
R.layout.layout_notification_expanded);
@ -284,41 +320,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
// expandedView.setTextViewText(R.id.info, infoMessage.toString());
// expandedView.setOnClickPendingIntent(R.id._tor_notificationBT, pendIntent);
expandedView.setImageViewResource(R.id.icon, icon);
if (mNotifyBuilder == null)
{
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (mNotifyBuilder == null)
{
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.app_name))
.setSmallIcon(R.drawable.ic_stat_tor);
mNotifyBuilder.setContentIntent(pendIntent);
}
}
mNotifyBuilder.setContentText(notifyMsg);
mNotifyBuilder.setSmallIcon(icon);
if (notifyType != NOTIFY_ID)
{
mNotifyBuilder.setTicker(notifyMsg);
// mNotifyBuilder.setLights(Color.GREEN, 1000, 1000);
}
else
{
mNotifyBuilder.setTicker(null);
}
mNotifyBuilder.setOngoing(prefPersistNotifications);
mNotification = mNotifyBuilder.build();
if (Build.VERSION.SDK_INT >= 16) {
mNotification.bigContentView = expandedView;
}
@ -565,8 +566,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
public void onCreate() {
super.onCreate();
// android.os.Debug.waitForDebugger();
try
{
initBinariesAndDirectories();
@ -577,6 +576,23 @@ public class TorService extends Service implements TorServiceConstants, TorConst
Log.e(TAG, "Error installing Orbot binaries",e);
logNotice("There was an error installing Orbot binaries");
}
mExecutor.execute(new Runnable ()
{
public void run ()
{
try
{
findExistingProc ();
}
catch (Exception e)
{
Log.e(TAG,"error onBind",e);
}
}
});
}
private void initBinariesAndDirectories () throws Exception
@ -971,7 +987,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
private synchronized int initControlConnection (int maxTries) throws Exception, RuntimeException
{
int i = 0;
int controlPort = getControlPort();
int controlPort = -1;
File fileCookie = new File(appCacheHome, TOR_CONTROL_COOKIE);
if (conn != null)
@ -1088,11 +1104,18 @@ public class TorService extends Service implements TorServiceConstants, TorConst
result = Integer.parseInt(lineParts[1]);
}
bufferedReader.close();
//store last valid control port
SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
prefs.edit().putInt("controlport", result).commit();
}
else
{
logNotice("Control Port config file does not yet exist (waiting for tor): " + fileControlPort.getCanonicalPath());
}
@ -1492,21 +1515,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
public IBinder onBind(Intent intent) {
mExecutor.execute(new Runnable ()
{
public void run ()
{
try
{
findExistingProc ();
}
catch (Exception e)
{
Log.e(TAG,"error onBind",e);
}
}
});
return mBinder;
}
@ -1798,8 +1806,6 @@ public class TorService extends Service implements TorServiceConstants, TorConst
boolean doNetworKSleep = prefs.getBoolean(TorConstants.PREF_DISABLE_NETWORK, true);
if (doNetworKSleep && mBinder != null)
{
final ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo netInfo = cm.getActiveNetworkInfo();
@ -1812,9 +1818,14 @@ public class TorService extends Service implements TorServiceConstants, TorConst
mConnectivity = false;
}
if (doNetworKSleep)
{
try {
if (mBinder != null)
{
mBinder.updateConfiguration("DisableNetwork", mConnectivity ? "0" : "1", false);
mBinder.saveConfiguration();
}
if (currentStatus == STATUS_ON)
{