2015-09-10 13:48:54 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
|
|
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.content.pm.ApplicationInfo;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.v4.app.NavUtils;
|
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2015-09-12 00:19:36 +02:00
|
|
|
import android.widget.AdapterView;
|
2015-09-10 13:48:54 +02:00
|
|
|
import android.widget.ArrayAdapter;
|
2015-09-12 00:19:36 +02:00
|
|
|
import android.widget.CheckBox;
|
2015-09-10 13:48:54 +02:00
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2015-12-22 14:09:14 +01:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Comparator;
|
2016-04-14 23:23:06 +02:00
|
|
|
import java.util.IdentityHashMap;
|
2015-09-10 13:48:54 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2015-09-25 00:53:40 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2015-09-10 13:48:54 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
|
|
|
|
|
2016-04-14 15:21:25 +02:00
|
|
|
public class AppBlacklistActivity extends GBActivity {
|
2015-09-10 13:48:54 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(AppBlacklistActivity.class);
|
|
|
|
|
2015-11-23 23:04:46 +01:00
|
|
|
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
2015-09-10 13:48:54 +02:00
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
String action = intent.getAction();
|
2015-12-07 23:33:32 +01:00
|
|
|
if (action.equals(GBApplication.ACTION_QUIT)) {
|
2015-09-10 13:48:54 +02:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-14 23:55:40 +02:00
|
|
|
private IdentityHashMap<ApplicationInfo, String> nameMap;
|
2015-09-12 00:19:36 +02:00
|
|
|
|
2015-09-10 13:48:54 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_appblacklist);
|
|
|
|
|
|
|
|
final PackageManager pm = getPackageManager();
|
2015-09-12 00:19:36 +02:00
|
|
|
|
2015-09-10 13:48:54 +02:00
|
|
|
final List<ApplicationInfo> packageList = pm.getInstalledApplications(PackageManager.GET_META_DATA);
|
|
|
|
ListView appListView = (ListView) findViewById(R.id.appListView);
|
|
|
|
|
2016-04-14 23:23:06 +02:00
|
|
|
// sort the package list by label and blacklist status
|
2016-04-14 23:55:40 +02:00
|
|
|
nameMap = new IdentityHashMap<>(packageList.size());
|
2016-04-14 23:23:06 +02:00
|
|
|
for (ApplicationInfo ai : packageList) {
|
|
|
|
CharSequence name = pm.getApplicationLabel(ai);
|
|
|
|
if (name == null) {
|
|
|
|
name = ai.packageName;
|
|
|
|
}
|
|
|
|
if (GBApplication.blacklist.contains(ai.packageName)) {
|
|
|
|
// sort blacklisted first by prefixing with a '!'
|
|
|
|
name = "!" + name;
|
|
|
|
}
|
|
|
|
nameMap.put(ai, name.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
Collections.sort(packageList, new Comparator<ApplicationInfo>() {
|
|
|
|
@Override
|
|
|
|
public int compare(ApplicationInfo ai1, ApplicationInfo ai2) {
|
|
|
|
final String s1 = nameMap.get(ai1);
|
|
|
|
final String s2 = nameMap.get(ai2);
|
|
|
|
return s1.compareTo(s2);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-09-12 00:19:36 +02:00
|
|
|
final ArrayAdapter<ApplicationInfo> adapter = new ArrayAdapter<ApplicationInfo>(this, R.layout.item_with_checkbox, packageList) {
|
2015-09-10 13:48:54 +02:00
|
|
|
@Override
|
|
|
|
public View getView(int position, View view, ViewGroup parent) {
|
|
|
|
if (view == null) {
|
|
|
|
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
2015-09-12 00:19:36 +02:00
|
|
|
view = inflater.inflate(R.layout.item_with_checkbox, parent, false);
|
2015-09-10 13:48:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ApplicationInfo appInfo = packageList.get(position);
|
2015-09-12 00:19:36 +02:00
|
|
|
TextView deviceAppVersionAuthorLabel = (TextView) view.findViewById(R.id.item_details);
|
2015-09-10 13:48:54 +02:00
|
|
|
TextView deviceAppNameLabel = (TextView) view.findViewById(R.id.item_name);
|
|
|
|
ImageView deviceImageView = (ImageView) view.findViewById(R.id.item_image);
|
2015-09-12 00:19:36 +02:00
|
|
|
CheckBox checkbox = (CheckBox) view.findViewById(R.id.item_checkbox);
|
2015-09-10 13:48:54 +02:00
|
|
|
|
2015-09-12 00:19:36 +02:00
|
|
|
deviceAppVersionAuthorLabel.setText(appInfo.packageName);
|
2016-04-14 23:55:40 +02:00
|
|
|
deviceAppNameLabel.setText(nameMap.get(appInfo));
|
2015-09-10 13:48:54 +02:00
|
|
|
deviceImageView.setImageDrawable(appInfo.loadIcon(pm));
|
|
|
|
|
2015-09-25 21:31:40 +02:00
|
|
|
checkbox.setChecked(GBApplication.blacklist.contains(appInfo.packageName));
|
2015-09-12 00:19:36 +02:00
|
|
|
|
2015-09-10 13:48:54 +02:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
appListView.setAdapter(adapter);
|
2015-09-12 00:19:36 +02:00
|
|
|
|
2015-09-10 13:48:54 +02:00
|
|
|
appListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView parent, View v, int position, long id) {
|
2015-09-12 00:19:36 +02:00
|
|
|
String packageName = packageList.get(position).packageName;
|
|
|
|
CheckBox checkBox = ((CheckBox) v.findViewById(R.id.item_checkbox));
|
|
|
|
checkBox.toggle();
|
|
|
|
if (checkBox.isChecked()) {
|
2015-09-25 00:53:40 +02:00
|
|
|
GBApplication.addToBlacklist(packageName);
|
2015-09-12 00:19:36 +02:00
|
|
|
} else {
|
2015-09-25 00:53:40 +02:00
|
|
|
GBApplication.removeFromBlacklist(packageName);
|
2015-09-12 00:19:36 +02:00
|
|
|
}
|
2015-09-10 13:48:54 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
IntentFilter filter = new IntentFilter();
|
2015-12-07 23:33:32 +01:00
|
|
|
filter.addAction(GBApplication.ACTION_QUIT);
|
2015-09-10 13:48:54 +02:00
|
|
|
|
|
|
|
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home:
|
|
|
|
NavUtils.navigateUpFromSameTask(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
|
|
|
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
}
|