Added hourglass to indicate the background service is busy

This commit is contained in:
M66B 2016-02-19 08:49:31 +01:00
parent e161c8d7ad
commit e6bdf5c77d
8 changed files with 67 additions and 20 deletions

View File

@ -65,6 +65,7 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
private boolean running = false;
private SwitchCompat swEnabled;
private ImageView ivQueue;
private ImageView ivMetered;
private SwipeRefreshLayout swipeRefresh;
private AdapterRule adapter = null;
@ -83,10 +84,14 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
private static final int MIN_SDK = Build.VERSION_CODES.ICE_CREAM_SANDWICH;
public static final String ACTION_RULES_CHANGED = "eu.faircode.netguard.ACTION_RULES_CHANGED";
public static final String ACTION_QUEUE_CHANGED = "eu.faircode.netguard.ACTION_QUEUE_CHANGED";
public static final String EXTRA_REFRESH = "Refresh";
public static final String EXTRA_SEARCH = "Search";
public static final String EXTRA_APPROVE = "Approve";
public static final String EXTRA_LOGCAT = "Logcat";
public static final String EXTRA_CONNECTED = "Connected";
public static final String EXTRA_METERED = "Metered";
public static final String EXTRA_SIZE = "Size";
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -123,6 +128,7 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
final View actionView = getLayoutInflater().inflate(R.layout.actionmain, null, false);
ImageView ivIcon = (ImageView) actionView.findViewById(R.id.ivIcon);
swEnabled = (SwitchCompat) actionView.findViewById(R.id.swEnabled);
ivQueue = (ImageView) actionView.findViewById(R.id.ivQueue);
ivMetered = (ImageView) actionView.findViewById(R.id.ivMetered);
// Icon, no title
@ -228,8 +234,12 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
prefs.registerOnSharedPreferenceChangeListener(this);
// Listen for rule set changes
IntentFilter iff = new IntentFilter(ACTION_RULES_CHANGED);
LocalBroadcastManager.getInstance(this).registerReceiver(onRulesetChanged, iff);
IntentFilter ifr = new IntentFilter(ACTION_RULES_CHANGED);
LocalBroadcastManager.getInstance(this).registerReceiver(onRulesChanged, ifr);
// Listen for queue changes
IntentFilter ifq = new IntentFilter(ACTION_QUEUE_CHANGED);
LocalBroadcastManager.getInstance(this).registerReceiver(onQueueChanged, ifq);
// Listen for added/removed applications
IntentFilter intentFilter = new IntentFilter();
@ -365,7 +375,8 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
LocalBroadcastManager.getInstance(this).unregisterReceiver(onRulesetChanged);
LocalBroadcastManager.getInstance(this).unregisterReceiver(onRulesChanged);
LocalBroadcastManager.getInstance(this).unregisterReceiver(onQueueChanged);
unregisterReceiver(packageChangedReceiver);
if (dialogFirst != null) {
@ -477,18 +488,18 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
}
};
private BroadcastReceiver onRulesetChanged = new BroadcastReceiver() {
private BroadcastReceiver onRulesChanged = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Received " + intent);
Util.logExtras(intent);
if (adapter != null)
if (intent.hasExtra("connected") && intent.hasExtra("metered")) {
ivMetered.setVisibility(Util.isMeteredNetwork(ActivityMain.this) ? View.VISIBLE : View.GONE);
if (intent.hasExtra(EXTRA_CONNECTED) && intent.hasExtra(EXTRA_METERED)) {
ivMetered.setVisibility(Util.isMeteredNetwork(ActivityMain.this) ? View.VISIBLE : View.INVISIBLE);
if (intent.getBooleanExtra("connected", false))
if (intent.getBooleanExtra("metered", false))
if (intent.getBooleanExtra(EXTRA_CONNECTED, false))
if (intent.getBooleanExtra(EXTRA_METERED, false))
adapter.setMobileActive();
else
adapter.setWifiActive();
@ -499,6 +510,16 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
}
};
private BroadcastReceiver onQueueChanged = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Received " + intent);
Util.logExtras(intent);
int size = intent.getIntExtra(EXTRA_SIZE, -1);
ivQueue.setVisibility(size == 0 ? View.INVISIBLE : View.VISIBLE);
}
};
private BroadcastReceiver packageChangedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {

View File

@ -179,10 +179,29 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
}
private final class CommandHandler extends Handler {
public int queue = 0;
public CommandHandler(Looper looper) {
super(looper);
}
private void reportQueueSize() {
Intent ruleset = new Intent(ActivityMain.ACTION_QUEUE_CHANGED);
ruleset.putExtra(ActivityMain.EXTRA_SIZE, queue);
LocalBroadcastManager.getInstance(SinkholeService.this).sendBroadcast(ruleset);
}
public void queue(Intent intent) {
synchronized (this) {
queue++;
reportQueueSize();
}
Message msg = commandHandler.obtainMessage();
msg.obj = intent;
msg.what = MSG_SERVICE_INTENT;
commandHandler.sendMessage(msg);
}
@Override
public void handleMessage(Message msg) {
try {
@ -197,6 +216,10 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
Util.sendCrashReport(ex, SinkholeService.this);
} finally {
synchronized (this) {
queue--;
reportQueueSize();
}
try {
PowerManager.WakeLock wl = getLock(SinkholeService.this);
if (wl.isHeld())
@ -295,8 +318,8 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
// Update main view
Intent ruleset = new Intent(ActivityMain.ACTION_RULES_CHANGED);
ruleset.putExtra("connected", last_connected);
ruleset.putExtra("metered", last_metered);
ruleset.putExtra(ActivityMain.EXTRA_CONNECTED, last_connected);
ruleset.putExtra(ActivityMain.EXTRA_METERED, last_metered);
LocalBroadcastManager.getInstance(SinkholeService.this).sendBroadcast(ruleset);
// Update widgets
@ -1571,12 +1594,7 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
Log.i(TAG, "Start intent=" + intent + " command=" + cmd + " reason=" + reason +
" vpn=" + (vpn != null) + " user=" + (Process.myUid() / 100000));
// Queue command
Message msg = commandHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
msg.what = MSG_SERVICE_INTENT;
commandHandler.sendMessage(msg);
commandHandler.queue(intent);
return START_STICKY;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 159 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 135 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 174 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 255 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 273 B

View File

@ -6,8 +6,8 @@
<ImageView
android:id="@+id/ivIcon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_security_white_24dp" />
@ -21,12 +21,20 @@
android:saveEnabled="false" />
<ImageView
android:id="@+id/ivMetered"
android:id="@+id/ivQueue"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:src="@drawable/ic_hourglass_empty_white_24dp"
android:visibility="invisible" />
<ImageView
android:id="@+id/ivMetered"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_attach_money_white_24dp"
android:visibility="gone" />
android:visibility="invisible" />
</LinearLayout>