improvements on notification code

This commit is contained in:
Nathan Freitas 2013-12-28 00:43:51 -05:00
parent 5891feee9a
commit c80c5d49df
1 changed files with 71 additions and 66 deletions

View File

@ -31,6 +31,7 @@ import org.torproject.android.TorConstants;
import org.torproject.android.Utils;
import org.torproject.android.settings.AppManager;
import android.annotation.SuppressLint;
import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
@ -43,12 +44,13 @@ import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.util.Log;
public class TorService extends Service implements TorServiceConstants, TorConstants, Runnable, EventHandler
@ -96,9 +98,11 @@ 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;
public void logMessage(String msg)
{
if (ENABLE_DEBUG_LOG)
@ -196,29 +200,19 @@ public class TorService extends Service implements TorServiceConstants, TorConst
}
@SuppressLint("NewApi")
private void showToolbarNotification (String notifyMsg, int notifyId, int icon, int flags)
{
if (mNotifyBuilder == null)
startNotification(getString(R.string.status_activated),prefPersistNotifications);
mNotifyBuilder.setContentText(notifyMsg);
mNotifyBuilder.setSmallIcon(icon);
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;
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);
mNotificationManager.notify(
NOTIFY_ID,
mNotifyBuilder.getNotification());
}
@ -857,7 +851,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
private void initControlConnection () throws Exception, RuntimeException
{
while (true)
while (conn == null)
{
try
{
@ -1010,10 +1004,16 @@ public class TorService extends Service implements TorServiceConstants, TorConst
}
}
NotificationCompat.Builder mNotifyBuilder;
Builder mNotifyBuilder;
@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);
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (mNotifyBuilder == null)
{
@ -1022,18 +1022,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
.setContentText( getString(R.string.status_activated))
.setSmallIcon(R.drawable.ic_stat_tor);
Intent intent = new Intent(TorService.this, Orbot.class);
PendingIntent pendIntent = PendingIntent.getActivity(TorService.this, 0, intent, 0);
mNotifyBuilder.setContentIntent(pendIntent);
}
if (mNotificationManager == null)
{
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
mNotifyBuilder.setOngoing(persistent);
@ -1042,6 +1031,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
mNotificationManager.notify(
NOTIFY_ID,
mNotifyBuilder.getNotification());
}
@ -1129,6 +1120,10 @@ 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;
@ -1228,6 +1223,13 @@ public class TorService extends Service implements TorServiceConstants, TorConst
_torInstance = this;
Thread thread = new Thread ()
{
public void run ()
{
try
{
initTorPaths();
@ -1237,6 +1239,9 @@ public class TorService extends Service implements TorServiceConstants, TorConst
{
Log.e(TAG,"error onBind",e);
}
}
};
thread.start();
if (ITorService.class.getName().equals(intent.getAction())) {
return mBinder;