1 /* Copyright 2016 Braden Farmer
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 package com.farmerbb.taskbar.adapter;
18 import android.content.Context;
19 import android.support.annotation.NonNull;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.ArrayAdapter;
24 import android.widget.CheckBox;
25 import android.widget.LinearLayout;
26 import android.widget.TextView;
27 import android.widget.Toast;
29 import com.farmerbb.taskbar.R;
30 import com.farmerbb.taskbar.util.Blacklist;
31 import com.farmerbb.taskbar.util.BlacklistEntry;
32 import com.farmerbb.taskbar.util.TopApps;
33 import com.farmerbb.taskbar.util.U;
35 import java.util.List;
37 public class AppListAdapter extends ArrayAdapter<BlacklistEntry> {
38 private final Blacklist blacklist = Blacklist.getInstance(getContext());
39 private final TopApps topApps = TopApps.getInstance(getContext());
41 private int type = -1;
43 public AppListAdapter(Context context, int layout, List<BlacklistEntry> list, int type) {
44 super(context, layout, list);
50 public @NonNull View getView(int position, View convertView, final @NonNull ViewGroup parent) {
51 // Check if an existing view is being reused, otherwise inflate the view
52 if(convertView == null)
53 convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_blacklist, parent, false);
57 setupHidden(position, convertView);
60 setupTopApps(position, convertView);
67 private void setupHidden(int position, View convertView) {
68 final BlacklistEntry entry = getItem(position);
71 final String componentName = entry.getPackageName();
72 final String componentNameAlt = componentName.contains("/") ? componentName.split("/")[1] : componentName;
74 TextView textView = U.findViewById(convertView, R.id.name);
75 textView.setText(entry.getLabel());
77 final CheckBox checkBox = U.findViewById(convertView, R.id.checkbox);
78 checkBox.setChecked(blacklist.isBlocked(componentName) || blacklist.isBlocked(componentNameAlt));
80 LinearLayout layout = U.findViewById(convertView, R.id.entry);
81 layout.setOnClickListener(view -> {
82 if(topApps.isTopApp(componentName) || topApps.isTopApp(componentNameAlt)) {
83 U.showToast(getContext(),
84 getContext().getString(R.string.already_top_app, entry.getLabel()),
86 } else if(blacklist.isBlocked(componentName)) {
87 blacklist.removeBlockedApp(getContext(), componentName);
88 checkBox.setChecked(false);
89 } else if(blacklist.isBlocked(componentNameAlt)) {
90 blacklist.removeBlockedApp(getContext(), componentNameAlt);
91 checkBox.setChecked(false);
93 blacklist.addBlockedApp(getContext(), entry);
94 checkBox.setChecked(true);
99 private void setupTopApps(int position, View convertView) {
100 final BlacklistEntry entry = getItem(position);
101 assert entry != null;
103 final String componentName = entry.getPackageName();
104 final String componentNameAlt = componentName.contains("/") ? componentName.split("/")[1] : componentName;
106 TextView textView = U.findViewById(convertView, R.id.name);
107 textView.setText(entry.getLabel());
109 final CheckBox checkBox = U.findViewById(convertView, R.id.checkbox);
110 checkBox.setChecked(topApps.isTopApp(componentName) || topApps.isTopApp(componentNameAlt));
112 LinearLayout layout = U.findViewById(convertView, R.id.entry);
113 layout.setOnClickListener(view -> {
114 if(blacklist.isBlocked(componentName) || blacklist.isBlocked(componentNameAlt)) {
115 U.showToast(getContext(),
116 getContext().getString(R.string.already_blacklisted, entry.getLabel()),
118 } else if(topApps.isTopApp(componentName)) {
119 topApps.removeTopApp(getContext(), componentName);
120 checkBox.setChecked(false);
121 } else if(topApps.isTopApp(componentNameAlt)) {
122 topApps.removeTopApp(getContext(), componentNameAlt);
123 checkBox.setChecked(false);
125 topApps.addTopApp(getContext(), entry);
126 checkBox.setChecked(true);