2018-02-26 14:27:32 +01:00
|
|
|
/* Copyright (C) 2017-2018 Carsten Pfeiffer, Daniele Gobbetti
|
2017-04-26 00:14:25 +02:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
2017-04-08 22:26:12 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.adapter;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.pm.ApplicationInfo;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.CheckBox;
|
2017-04-09 00:21:43 +02:00
|
|
|
import android.widget.Filter;
|
|
|
|
import android.widget.Filterable;
|
2017-04-08 22:26:12 +02:00
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2017-04-09 00:21:43 +02:00
|
|
|
import java.util.ArrayList;
|
2017-04-08 22:26:12 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.IdentityHashMap;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
|
2018-06-18 20:38:37 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.GBApplication.packageNameToPebbleMsgSender;
|
|
|
|
|
2017-04-09 00:21:43 +02:00
|
|
|
public class AppBlacklistAdapter extends RecyclerView.Adapter<AppBlacklistAdapter.AppBLViewHolder> implements Filterable {
|
2017-04-08 22:26:12 +02:00
|
|
|
|
2017-04-09 00:21:43 +02:00
|
|
|
private List<ApplicationInfo> applicationInfoList;
|
2017-04-08 22:26:12 +02:00
|
|
|
private final int mLayoutId;
|
|
|
|
private final Context mContext;
|
|
|
|
private final PackageManager mPm;
|
|
|
|
private final IdentityHashMap<ApplicationInfo, String> mNameMap;
|
|
|
|
|
2017-04-09 00:21:43 +02:00
|
|
|
private ApplicationFilter applicationFilter;
|
2017-04-08 22:26:12 +02:00
|
|
|
|
|
|
|
public AppBlacklistAdapter(int layoutId, Context context) {
|
|
|
|
mLayoutId = layoutId;
|
|
|
|
mContext = context;
|
|
|
|
mPm = context.getPackageManager();
|
|
|
|
|
|
|
|
applicationInfoList = mPm.getInstalledApplications(PackageManager.GET_META_DATA);
|
|
|
|
|
|
|
|
// sort the package list by label and blacklist status
|
2017-04-22 16:59:55 +02:00
|
|
|
mNameMap = new IdentityHashMap<>(applicationInfoList.size());
|
2017-04-08 22:26:12 +02:00
|
|
|
for (ApplicationInfo ai : applicationInfoList) {
|
|
|
|
CharSequence name = mPm.getApplicationLabel(ai);
|
|
|
|
if (name == null) {
|
|
|
|
name = ai.packageName;
|
|
|
|
}
|
2018-06-18 20:38:37 +02:00
|
|
|
if (GBApplication.appIsNotifBlacklisted(ai.packageName) || GBApplication.appIsPebbleBlacklisted(packageNameToPebbleMsgSender(ai.packageName))) {
|
2017-04-08 22:26:12 +02:00
|
|
|
// sort blacklisted first by prefixing with a '!'
|
|
|
|
name = "!" + name;
|
|
|
|
}
|
|
|
|
mNameMap.put(ai, name.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
Collections.sort(applicationInfoList, new Comparator<ApplicationInfo>() {
|
|
|
|
@Override
|
|
|
|
public int compare(ApplicationInfo ai1, ApplicationInfo ai2) {
|
|
|
|
final String s1 = mNameMap.get(ai1);
|
|
|
|
final String s2 = mNameMap.get(ai2);
|
|
|
|
return s1.compareTo(s2);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public AppBlacklistAdapter.AppBLViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
|
|
|
View view = LayoutInflater.from(mContext).inflate(mLayoutId, parent, false);
|
|
|
|
return new AppBLViewHolder(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(AppBlacklistAdapter.AppBLViewHolder holder, int position) {
|
|
|
|
final ApplicationInfo appInfo = applicationInfoList.get(position);
|
|
|
|
|
|
|
|
holder.deviceAppVersionAuthorLabel.setText(appInfo.packageName);
|
|
|
|
holder.deviceAppNameLabel.setText(mNameMap.get(appInfo));
|
|
|
|
holder.deviceImageView.setImageDrawable(appInfo.loadIcon(mPm));
|
|
|
|
|
2018-06-18 20:38:37 +02:00
|
|
|
holder.blacklist_checkbox.setChecked(GBApplication.appIsNotifBlacklisted(appInfo.packageName));
|
|
|
|
holder.blacklist_checkbox.setTextColor(mContext.getResources().getColorStateList(R.color.blacklist_checkboxes));
|
|
|
|
|
|
|
|
holder.blacklist_pebble_checkbox.setChecked(GBApplication.appIsPebbleBlacklisted(packageNameToPebbleMsgSender(appInfo.packageName)));
|
2017-04-08 22:26:12 +02:00
|
|
|
|
2018-06-18 20:38:37 +02:00
|
|
|
holder.blacklist_pebble_checkbox.setTextColor(mContext.getResources().getColorStateList(R.color.blacklist_checkboxes));
|
|
|
|
holder.blacklist_pebble_checkbox.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
if ( ((CheckBox)view).isChecked() ) {
|
|
|
|
GBApplication.addAppToPebbleBlacklist(appInfo.packageName);
|
|
|
|
} else {
|
|
|
|
GBApplication.removeFromAppsPebbleBlacklist(appInfo.packageName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-04-08 22:26:12 +02:00
|
|
|
holder.itemView.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
CheckBox checkBox = ((CheckBox) v.findViewById(R.id.item_checkbox));
|
|
|
|
checkBox.toggle();
|
|
|
|
if (checkBox.isChecked()) {
|
2018-06-18 20:38:37 +02:00
|
|
|
GBApplication.addAppToNotifBlacklist(appInfo.packageName);
|
2017-04-08 22:26:12 +02:00
|
|
|
} else {
|
2018-06-18 20:38:37 +02:00
|
|
|
GBApplication.removeFromAppsNotifBlacklist(appInfo.packageName);
|
2017-04-08 22:26:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return applicationInfoList.size();
|
|
|
|
}
|
|
|
|
|
2017-04-09 00:21:43 +02:00
|
|
|
@Override
|
|
|
|
public Filter getFilter() {
|
|
|
|
if (applicationFilter == null)
|
|
|
|
applicationFilter = new ApplicationFilter(this, applicationInfoList);
|
|
|
|
return applicationFilter;
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:38:37 +02:00
|
|
|
class AppBLViewHolder extends RecyclerView.ViewHolder {
|
2017-04-08 22:26:12 +02:00
|
|
|
|
2018-06-18 20:38:37 +02:00
|
|
|
final CheckBox blacklist_checkbox;
|
|
|
|
final CheckBox blacklist_pebble_checkbox;
|
2017-04-08 22:26:12 +02:00
|
|
|
final ImageView deviceImageView;
|
|
|
|
final TextView deviceAppVersionAuthorLabel;
|
|
|
|
final TextView deviceAppNameLabel;
|
|
|
|
|
|
|
|
AppBLViewHolder(View itemView) {
|
|
|
|
super(itemView);
|
|
|
|
|
2018-06-18 20:38:37 +02:00
|
|
|
blacklist_checkbox = (CheckBox) itemView.findViewById(R.id.item_checkbox);
|
|
|
|
blacklist_pebble_checkbox = (CheckBox) itemView.findViewById(R.id.item_pebble_checkbox);
|
2017-04-08 22:26:12 +02:00
|
|
|
deviceImageView = (ImageView) itemView.findViewById(R.id.item_image);
|
|
|
|
deviceAppVersionAuthorLabel = (TextView) itemView.findViewById(R.id.item_details);
|
|
|
|
deviceAppNameLabel = (TextView) itemView.findViewById(R.id.item_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-09 00:21:43 +02:00
|
|
|
private class ApplicationFilter extends Filter {
|
|
|
|
|
|
|
|
private final AppBlacklistAdapter adapter;
|
|
|
|
private final List<ApplicationInfo> originalList;
|
|
|
|
private final List<ApplicationInfo> filteredList;
|
|
|
|
|
|
|
|
private ApplicationFilter(AppBlacklistAdapter adapter, List<ApplicationInfo> originalList) {
|
|
|
|
super();
|
|
|
|
this.originalList = new ArrayList<>(originalList);
|
|
|
|
this.filteredList = new ArrayList<>();
|
|
|
|
this.adapter = adapter;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Filter.FilterResults performFiltering(CharSequence filter) {
|
|
|
|
filteredList.clear();
|
|
|
|
final Filter.FilterResults results = new Filter.FilterResults();
|
|
|
|
|
|
|
|
if (filter == null || filter.length() == 0)
|
|
|
|
filteredList.addAll(originalList);
|
|
|
|
else {
|
|
|
|
final String filterPattern = filter.toString().toLowerCase().trim();
|
|
|
|
|
|
|
|
for (ApplicationInfo ai : originalList) {
|
|
|
|
CharSequence name = mPm.getApplicationLabel(ai);
|
2017-04-22 21:16:40 +02:00
|
|
|
if (name.toString().contains(filterPattern) ||
|
2017-04-09 00:21:43 +02:00
|
|
|
(ai.packageName.contains(filterPattern))) {
|
|
|
|
filteredList.add(ai);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
results.values = filteredList;
|
|
|
|
results.count = filteredList.size();
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void publishResults(CharSequence charSequence, Filter.FilterResults filterResults) {
|
|
|
|
adapter.applicationInfoList.clear();
|
|
|
|
adapter.applicationInfoList.addAll((List<ApplicationInfo>) filterResults.values);
|
|
|
|
adapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-08 22:26:12 +02:00
|
|
|
}
|