OSDN Git Service

13691de24c3b99135bc210590fd93cb8bc7cc5d9
[android-x86/packages-apps-Taskbar.git] / app / src / main / java / com / farmerbb / taskbar / adapter / AppListAdapter.java
1 /* Copyright 2016 Braden Farmer
2  *
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
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 package com.farmerbb.taskbar.adapter;
17
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;
28
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;
34
35 import java.util.List;
36
37 public class AppListAdapter extends ArrayAdapter<BlacklistEntry> {
38     private final Blacklist blacklist = Blacklist.getInstance(getContext());
39     private final TopApps topApps = TopApps.getInstance(getContext());
40
41     private int type = -1;
42
43     public AppListAdapter(Context context, int layout, List<BlacklistEntry> list, int type) {
44         super(context, layout, list);
45
46         this.type = type;
47     }
48
49     @Override
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);
54
55         switch(type) {
56             case U.HIDDEN:
57                 setupHidden(position, convertView);
58                 break;
59             case U.TOP_APPS:
60                 setupTopApps(position, convertView);
61                 break;
62         }
63
64         return convertView;
65     }
66
67     private void setupHidden(int position, View convertView) {
68         final BlacklistEntry entry = getItem(position);
69         assert entry != null;
70
71         final String componentName = entry.getPackageName();
72         final String componentNameAlt = componentName.contains("/") ? componentName.split("/")[1] : componentName;
73
74         TextView textView = U.findViewById(convertView, R.id.name);
75         textView.setText(entry.getLabel());
76
77         final CheckBox checkBox = U.findViewById(convertView, R.id.checkbox);
78         checkBox.setChecked(blacklist.isBlocked(componentName) || blacklist.isBlocked(componentNameAlt));
79
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()),
85                         Toast.LENGTH_LONG);
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);
92             } else {
93                 blacklist.addBlockedApp(getContext(), entry);
94                 checkBox.setChecked(true);
95             }
96         });
97     }
98
99     private void setupTopApps(int position, View convertView) {
100         final BlacklistEntry entry = getItem(position);
101         assert entry != null;
102
103         final String componentName = entry.getPackageName();
104         final String componentNameAlt = componentName.contains("/") ? componentName.split("/")[1] : componentName;
105
106         TextView textView = U.findViewById(convertView, R.id.name);
107         textView.setText(entry.getLabel());
108
109         final CheckBox checkBox = U.findViewById(convertView, R.id.checkbox);
110         checkBox.setChecked(topApps.isTopApp(componentName) || topApps.isTopApp(componentNameAlt));
111
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()),
117                         Toast.LENGTH_LONG);
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);
124             } else {
125                 topApps.addTopApp(getContext(), entry);
126                 checkBox.setChecked(true);
127             }
128         });
129     }
130 }