OSDN Git Service

No need for findViewById wrapper methods anymore
[android-x86/packages-apps-Taskbar.git] / app / src / main / java / com / farmerbb / taskbar / activity / IconPackActivity.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.activity;
17
18 import android.content.ActivityNotFoundException;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.SharedPreferences;
22 import android.content.pm.PackageManager;
23 import android.os.AsyncTask;
24 import android.os.Bundle;
25 import android.support.annotation.NonNull;
26 import android.support.v7.app.AppCompatActivity;
27 import android.view.LayoutInflater;
28 import android.view.MotionEvent;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.ArrayAdapter;
32 import android.widget.ImageView;
33 import android.widget.LinearLayout;
34 import android.widget.ListView;
35 import android.widget.ProgressBar;
36 import android.widget.TextView;
37
38 import com.farmerbb.taskbar.BuildConfig;
39 import com.farmerbb.taskbar.R;
40 import com.farmerbb.taskbar.util.IconPack;
41 import com.farmerbb.taskbar.util.IconPackManager;
42 import com.farmerbb.taskbar.util.U;
43
44 import java.text.Collator;
45 import java.util.ArrayList;
46 import java.util.Collections;
47 import java.util.List;
48
49 public class IconPackActivity extends AppCompatActivity {
50
51     private AppListGenerator appListGenerator;
52     private ProgressBar progressBar;
53     private ListView appList;
54
55     @Override
56     protected void onCreate(Bundle savedInstanceState) {
57         super.onCreate(savedInstanceState);
58
59         setContentView(R.layout.select_app);
60         setFinishOnTouchOutside(false);
61         setTitle(R.string.icon_pack);
62
63         progressBar = findViewById(R.id.progress_bar);
64         appList = findViewById(R.id.list);
65
66         appListGenerator = new AppListGenerator();
67         appListGenerator.execute();
68     }
69
70     @Override
71     public void finish() {
72         if(appListGenerator != null && appListGenerator.getStatus() == AsyncTask.Status.RUNNING)
73             appListGenerator.cancel(true);
74
75         super.finish();
76     }
77
78     private void openIconPackActivity(String packageName) {
79         Intent intent = new Intent("org.adw.launcher.THEMES");
80         intent.setPackage(packageName);
81
82         try {
83             startActivity(intent);
84         } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
85     }
86
87     private class AppListAdapter extends ArrayAdapter<IconPack> {
88         AppListAdapter(Context context, int layout, List<IconPack> list) {
89             super(context, layout, list);
90         }
91
92         @Override
93         public @NonNull View getView(int position, View convertView, final @NonNull ViewGroup parent) {
94             // Check if an existing view is being reused, otherwise inflate the view
95             if(convertView == null)
96                 convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, parent, false);
97
98             final IconPack entry = getItem(position);
99             assert entry != null;
100
101             TextView textView = convertView.findViewById(R.id.name);
102             textView.setText(entry.getName());
103
104             PackageManager pm = getPackageManager();
105             ImageView imageView = convertView.findViewById(R.id.icon);
106
107             if(entry.getPackageName().equals(BuildConfig.APPLICATION_ID)) {
108                 imageView.setImageDrawable(null);
109             } else {
110                 try {
111                     imageView.setImageDrawable(pm.getApplicationIcon(entry.getPackageName()));
112                 } catch (PackageManager.NameNotFoundException e) {
113                     imageView.setImageDrawable(pm.getDefaultActivityIcon());
114                 }
115             }
116
117             LinearLayout layout = convertView.findViewById(R.id.entry);
118             layout.setOnClickListener(view -> {
119                 SharedPreferences pref = U.getSharedPreferences(IconPackActivity.this);
120                 pref.edit().putString("icon_pack", entry.getPackageName()).apply();
121                 setResult(RESULT_OK);
122                 finish();
123             });
124
125             layout.setOnLongClickListener(v -> {
126                 openIconPackActivity(entry.getPackageName());
127                 return false;
128             });
129
130             layout.setOnGenericMotionListener((view, motionEvent) -> {
131                 if(motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS
132                         && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY) {
133                     openIconPackActivity(entry.getPackageName());
134                 }
135                 return false;
136             });
137
138             return convertView;
139         }
140     }
141
142     private final class AppListGenerator extends AsyncTask<Void, Void, AppListAdapter> {
143         @Override
144         protected AppListAdapter doInBackground(Void... params) {
145             List<IconPack> list = IconPackManager.getInstance().getAvailableIconPacks(IconPackActivity.this);
146             if(list.isEmpty())
147                 return null;
148             else {
149                 List<IconPack> finalList = new ArrayList<>();
150                 IconPack dummyIconPack = new IconPack();
151                 dummyIconPack.setPackageName(BuildConfig.APPLICATION_ID);
152                 dummyIconPack.setName(getString(R.string.icon_pack_none));
153
154                 Collections.sort(list, (ip1, ip2) -> Collator.getInstance().compare(ip1.getName(), ip2.getName()));
155
156                 finalList.add(dummyIconPack);
157                 finalList.addAll(list);
158
159                 return new AppListAdapter(IconPackActivity.this, R.layout.row, finalList);
160             }
161         }
162
163         @Override
164         protected void onPostExecute(AppListAdapter adapter) {
165             if(adapter == null) {
166                 U.showToast(IconPackActivity.this, R.string.no_icon_packs_installed);
167                 setResult(RESULT_CANCELED);
168                 finish();
169             } else {
170                 progressBar.setVisibility(View.GONE);
171                 appList.setAdapter(adapter);
172                 setFinishOnTouchOutside(true);
173             }
174         }
175     }
176 }