FilterApp: improvements

This commit is contained in:
PeratX 2019-07-14 20:37:28 +08:00
parent 4d9045655f
commit 2124ab2d69

View File

@ -12,16 +12,6 @@ import android.view.ViewGroup;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
@ -30,6 +20,13 @@ import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat; import androidx.core.graphics.drawable.DrawableCompat;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
/** /**
* Daedalus Project * Daedalus Project
@ -63,8 +60,12 @@ public class FilterAppProxyActivity extends AppCompatActivity {
DrawableCompat.setTint(wrappedDrawable, Color.WHITE); DrawableCompat.setTint(wrappedDrawable, Color.WHITE);
toolbar.setNavigationIcon(drawable); toolbar.setNavigationIcon(drawable);
toolbar.setNavigationOnClickListener(v -> onBackPressed()); toolbar.setNavigationOnClickListener(v -> onBackPressed());
adapter = new RecyclerViewAdapter(getAppList()); adapter = new RecyclerViewAdapter();
recyclerView.setAdapter(adapter); recyclerView.setAdapter(adapter);
new Thread(() -> {
ArrayList<AppObject> appList = getAppList();
adapter.updateList(appList);
}).start();
} }
@Override @Override
@ -87,14 +88,14 @@ public class FilterAppProxyActivity extends AppCompatActivity {
} }
private class AppObject { private class AppObject {
private String app_name; private String appName;
private String app_package_name; private String appPackageName;
private Drawable app_icon; private Drawable appIcon;
AppObject(String appName, String packageName, Drawable appIcon) { AppObject(String appName, String packageName, Drawable appIcon) {
this.app_name = appName; this.appName = appName;
this.app_package_name = packageName; this.appPackageName = packageName;
this.app_icon = appIcon; this.appIcon = appIcon;
} }
} }
@ -113,20 +114,21 @@ public class FilterAppProxyActivity extends AppCompatActivity {
} }
private class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolder> { private class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {
private ArrayList<AppObject> appList; private ArrayList<AppObject> appList = new ArrayList<>();
@SuppressLint("UseSparseArrays") @SuppressLint("UseSparseArrays")
private Map<Integer, Boolean> check_status = new HashMap<>(); private HashMap<Integer, Boolean> checkStatus = new HashMap<>();
RecyclerViewAdapter(ArrayList<AppObject> appObjects) { void updateList(ArrayList<AppObject> appObjects) {
appList = appObjects; appList = appObjects;
for (int i = 0; i < appObjects.size(); i++) { for (int i = 0; i < appObjects.size(); i++) {
if (Daedalus.configurations.getFilterAppObjects().contains(appObjects.get(i).app_package_name)) { if (Daedalus.configurations.getFilterAppObjects().contains(appObjects.get(i).appPackageName)) {
check_status.put(i, true); checkStatus.put(i, true);
}
} }
} }
runOnUiThread(() -> notifyDataSetChanged());
}
@NonNull @NonNull
@Override @Override
@ -137,26 +139,25 @@ public class FilterAppProxyActivity extends AppCompatActivity {
@Override @Override
public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) { public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
String package_name = appList.get(position).app_package_name; String packageName = appList.get(position).appPackageName;
holder.app_name.setText(appList.get(position).app_name); holder.appName.setText(appList.get(position).appName);
holder.app_icon.setImageDrawable(appList.get(position).app_icon); holder.appIcon.setImageDrawable(appList.get(position).appIcon);
holder.app_package_name = package_name; holder.appPackageName = packageName;
holder.app_check.setOnCheckedChangeListener(null); holder.appCheck.setOnCheckedChangeListener(null);
if (Daedalus.configurations.getFilterAppObjects().contains(package_name)) { if (Daedalus.configurations.getFilterAppObjects().contains(packageName)) {
holder.app_check.setChecked(true); holder.appCheck.setChecked(true);
//check_status.put(position, true);
} }
holder.app_check.setOnCheckedChangeListener((buttonView, isChecked) -> { holder.appCheck.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) { if (isChecked) {
check_status.put(position, true); checkStatus.put(position, true);
} else { } else {
check_status.remove(position); checkStatus.remove(position);
} }
}); });
if (check_status != null && check_status.containsKey(position)) { if (checkStatus != null && checkStatus.containsKey(position)) {
holder.app_check.setChecked(true); holder.appCheck.setChecked(true);
} else { } else {
holder.app_check.setChecked(false); holder.appCheck.setChecked(false);
} }
} }
@ -167,28 +168,28 @@ public class FilterAppProxyActivity extends AppCompatActivity {
} }
private class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { private class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView app_icon; private ImageView appIcon;
private TextView app_name; private TextView appName;
private CheckBox app_check; private CheckBox appCheck;
private String app_package_name; private String appPackageName;
RecyclerViewHolder(@NonNull View itemView) { RecyclerViewHolder(@NonNull View itemView) {
super(itemView); super(itemView);
app_icon = itemView.findViewById(R.id.app_icon); appIcon = itemView.findViewById(R.id.app_icon);
app_name = itemView.findViewById(R.id.app_name); appName = itemView.findViewById(R.id.app_name);
app_check = itemView.findViewById(R.id.app_check); appCheck = itemView.findViewById(R.id.app_check);
itemView.setOnClickListener(this); itemView.setOnClickListener(this);
} }
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (app_check.isChecked()) { if (appCheck.isChecked()) {
app_check.setChecked(false); appCheck.setChecked(false);
Daedalus.configurations.getFilterAppObjects().remove(app_package_name); Daedalus.configurations.getFilterAppObjects().remove(appPackageName);
} else { } else {
app_check.setChecked(true); appCheck.setChecked(true);
Daedalus.configurations.getFilterAppObjects().add(app_package_name); Daedalus.configurations.getFilterAppObjects().add(appPackageName);
} }
} }
} }