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.activity;
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;
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;
44 import java.text.Collator;
45 import java.util.ArrayList;
46 import java.util.Collections;
47 import java.util.List;
49 public class IconPackActivity extends AppCompatActivity {
51 private AppListGenerator appListGenerator;
52 private ProgressBar progressBar;
53 private ListView appList;
56 protected void onCreate(Bundle savedInstanceState) {
57 super.onCreate(savedInstanceState);
59 setContentView(R.layout.select_app);
60 setFinishOnTouchOutside(false);
61 setTitle(R.string.icon_pack);
63 progressBar = findViewById(R.id.progress_bar);
64 appList = findViewById(R.id.list);
66 appListGenerator = new AppListGenerator();
67 appListGenerator.execute();
71 public void finish() {
72 if(appListGenerator != null && appListGenerator.getStatus() == AsyncTask.Status.RUNNING)
73 appListGenerator.cancel(true);
78 private void openIconPackActivity(String packageName) {
79 Intent intent = new Intent("org.adw.launcher.THEMES");
80 intent.setPackage(packageName);
83 startActivity(intent);
84 } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
87 private class AppListAdapter extends ArrayAdapter<IconPack> {
88 AppListAdapter(Context context, int layout, List<IconPack> list) {
89 super(context, layout, list);
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);
98 final IconPack entry = getItem(position);
101 TextView textView = convertView.findViewById(R.id.name);
102 textView.setText(entry.getName());
104 PackageManager pm = getPackageManager();
105 ImageView imageView = convertView.findViewById(R.id.icon);
107 if(entry.getPackageName().equals(BuildConfig.APPLICATION_ID)) {
108 imageView.setImageDrawable(null);
111 imageView.setImageDrawable(pm.getApplicationIcon(entry.getPackageName()));
112 } catch (PackageManager.NameNotFoundException e) {
113 imageView.setImageDrawable(pm.getDefaultActivityIcon());
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);
125 layout.setOnLongClickListener(v -> {
126 openIconPackActivity(entry.getPackageName());
130 layout.setOnGenericMotionListener((view, motionEvent) -> {
131 if(motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS
132 && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY) {
133 openIconPackActivity(entry.getPackageName());
142 private final class AppListGenerator extends AsyncTask<Void, Void, AppListAdapter> {
144 protected AppListAdapter doInBackground(Void... params) {
145 List<IconPack> list = IconPackManager.getInstance().getAvailableIconPacks(IconPackActivity.this);
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));
154 Collections.sort(list, (ip1, ip2) -> Collator.getInstance().compare(ip1.getName(), ip2.getName()));
156 finalList.add(dummyIconPack);
157 finalList.addAll(list);
159 return new AppListAdapter(IconPackActivity.this, R.layout.row, finalList);
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);
170 progressBar.setVisibility(View.GONE);
171 appList.setAdapter(adapter);
172 setFinishOnTouchOutside(true);