Skip to content

Commit

Permalink
Move Notification handling to a single util class. Fixed relaunching of
Browse files Browse the repository at this point in the history
the auto connect service when it is cancelled before the waiitng period.
  • Loading branch information
ruiaraujo committed Nov 19, 2013
1 parent 7c254d9 commit 89d70b0
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 178 deletions.
4 changes: 3 additions & 1 deletion android/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<uses-feature
android:name="android.hardware.touchscreen"
android:required="false" />

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Expand Down Expand Up @@ -77,6 +77,8 @@
<activity
android:name=".CancelOperationActivity"
android:excludeFromRecents="true"
android:launchMode="singleTask"
android:taskAffinity=""
android:theme="@style/Theme.Sherlock.Dialog" />
<activity
android:name="com.ipaulpro.afilechooser.FileChooserActivity"
Expand Down
1 change: 1 addition & 0 deletions android/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

<style name="Theme.Sherlock.Dialog" parent="Theme.Sherlock.NoActionBar">
<item name="android:windowIsFloating">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>

</resources>
3 changes: 2 additions & 1 deletion android/src/org/exobel/routerkeygen/AutoConnectManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public void onReceive(Context context, Intent intent) {
SupplicantState state = intent
.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
if (state != null) {
Log.d(this.getClass().getSimpleName(), state.name());
if (BuildConfig.DEBUG)
Log.d(this.getClass().getSimpleName(), state.name());
if (state.equals(SupplicantState.COMPLETED)) {
listener.onSuccessfulConection();
return;
Expand Down
130 changes: 55 additions & 75 deletions android/src/org/exobel/routerkeygen/AutoConnectService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
package org.exobel.routerkeygen;

import java.util.List;

import org.exobel.routerkeygen.AutoConnectManager.onConnectionListener;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
Expand All @@ -38,9 +39,8 @@
import android.os.Handler;
import android.os.IBinder;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.RemoteViews;

import com.farproc.wifi.connecter.Wifi;

public class AutoConnectService extends Service implements onConnectionListener {
Expand All @@ -55,6 +55,7 @@ public class AutoConnectService extends Service implements onConnectionListener
+ AutoConnectService.class.getName().hashCode();

private NotificationManager mNotificationManager;
private Handler handler;

final private Binder mBinder = new LocalBinder();
private ScanResult network;
Expand All @@ -66,6 +67,12 @@ public class AutoConnectService extends Service implements onConnectionListener
private int currentNetworkId = -1;
private boolean cancelNotification = true;

private Runnable tryAfterDisconnecting = new Runnable() {
public void run() {
tryingConnection();
}
};

@Override
public IBinder onBind(Intent intent) {
return mBinder;
Expand All @@ -84,6 +91,7 @@ AutoConnectService getService() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@SuppressWarnings("deprecation")
public void onCreate() {
handler = new Handler();
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Expand Down Expand Up @@ -118,21 +126,23 @@ public int onStartCommand(Intent intent, int flags, int startId) {
network.capabilities);
mNotificationManager
.notify(UNIQUE_ID,
createProgressBar(
getString(R.string.app_name),
getString(R.string.not_auto_connect_waiting),
0));
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
tryingConnection();
}
}, DISCONNECT_WAITING_TIME);
NotificationUtils
.createProgressBar(
this,
getString(R.string.app_name),
getString(R.string.not_auto_connect_waiting),
keys.size(),
0,
false,
getDefaultPendingIntent(getApplicationContext())));
handler.postDelayed(tryAfterDisconnecting,
DISCONNECT_WAITING_TIME);
cancelNotification = true;
} else {
mNotificationManager.notify(
UNIQUE_ID,
getSimple(getString(R.string.msg_error),
NotificationUtils.getSimple(this,
getString(R.string.msg_error),
getString(R.string.msg_error_key_testing))
.build());
cancelNotification = false;
Expand All @@ -154,30 +164,36 @@ private void tryingConnection() {

if (currentNetworkId != -1) {
lastTimeDisconnected = System.currentTimeMillis();
registerReceiver(mReceiver, new IntentFilter(
WifiManager.SUPPLICANT_STATE_CHANGED_ACTION));
mNotificationManager.notify(
UNIQUE_ID,
createProgressBar(
if (attempts == 1)// first try, we register the listener
registerReceiver(mReceiver, new IntentFilter(
WifiManager.SUPPLICANT_STATE_CHANGED_ACTION));
mNotificationManager.notify(UNIQUE_ID, NotificationUtils
.createProgressBar(
this,
getString(R.string.app_name),
getString(R.string.not_auto_connect_key_testing,
keys.get(attempts - 1)), attempts));
keys.get(attempts - 1)), keys.size(),
attempts, false,
getDefaultPendingIntent(getApplicationContext())));
cancelNotification = true;
} else {
mNotificationManager.notify(
UNIQUE_ID,
getSimple(getString(R.string.msg_error),
NotificationUtils.getSimple(this,
getString(R.string.msg_error),
getString(R.string.msg_error_key_testing)).build());
cancelNotification = false;
stopSelf();
}
}

public void onDestroy() {
super.onDestroy();
handler.removeCallbacks(tryAfterDisconnecting);
if (cancelNotification)
mNotificationManager.cancel(UNIQUE_ID);
reenableAllHotspots();
try {
if (cancelNotification)
mNotificationManager.cancel(UNIQUE_ID);
reenableAllHotspots();
unregisterReceiver(mReceiver);
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -198,7 +214,8 @@ public void onFailedConnection() {
reenableAllHotspots();
mNotificationManager.notify(
UNIQUE_ID,
getSimple(getString(R.string.msg_error),
NotificationUtils.getSimple(this,
getString(R.string.msg_error),
getString(R.string.msg_no_correct_keys)).build());
cancelNotification = false;
stopSelf();
Expand All @@ -211,7 +228,8 @@ public void onSuccessfulConection() {
reenableAllHotspots();
mNotificationManager.notify(
UNIQUE_ID,
getSimple(
NotificationUtils.getSimple(
this,
getString(R.string.app_name),
getString(R.string.not_correct_key_testing,
keys.get(attempts - 1))).build());
Expand All @@ -230,55 +248,17 @@ private void reenableAllHotspots() {
}
}

private NotificationCompat.Builder getSimple(CharSequence title,
CharSequence context) {
return new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification).setTicker(title)
.setContentTitle(title).setContentText(context)
.setContentIntent(getPendingIntent());
}

private Notification createProgressBar(CharSequence title,
CharSequence content, int progress) {
final NotificationCompat.Builder builder = getSimple(title, content);
final PendingIntent i = PendingIntent.getActivity(
getApplicationContext(),
0,
new Intent(this, CancelOperationActivity.class)
.putExtra(CancelOperationActivity.SERVICE_TO_TERMINATE,
AutoConnectService.class.getName())
.putExtra(CancelOperationActivity.MESSAGE,
getString(R.string.cancel_auto_test))
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private static PendingIntent getDefaultPendingIntent(Context context) {
final Intent i = new Intent(context, CancelOperationActivity.class)
.putExtra(CancelOperationActivity.SERVICE_TO_TERMINATE,
AutoConnectService.class.getName())
.putExtra(CancelOperationActivity.MESSAGE,
context.getString(R.string.cancel_auto_test))
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB)
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
return PendingIntent.getActivity(context, 0, i,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(i);
builder.setOngoing(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
builder.setProgress(keys.size(), progress, false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
builder.addAction(
android.R.drawable.ic_menu_close_clear_cancel,
getString(android.R.string.cancel), i);
}
} else {
final RemoteViews contentView = new RemoteViews(getPackageName(),
R.layout.notification);
contentView.setTextViewText(R.id.text1, content);
contentView.setProgressBar(R.id.progress, keys.size(), progress,
false);
final Notification not = builder.build();
not.contentView = contentView;
return not;
}
return builder.build();
}

private PendingIntent getPendingIntent() {
return PendingIntent.getActivity(getApplicationContext(), 0,
new Intent(), // add this
// pass null
// to intent
PendingIntent.FLAG_UPDATE_CURRENT);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ protected Dialog onCreateDialog(int id) {
String message = getIntent().getStringExtra(MESSAGE);
if (message == null)
message = getString(android.R.string.cancel) + "?";

builder.setTitle(R.string.app_name)
.setNegativeButton(android.R.string.no,
new DialogInterface.OnClickListener() {
Expand Down
Loading

0 comments on commit 89d70b0

Please sign in to comment.