OSDN Git Service

Filter out Shell from list of launchers in HSLConfigActivity
[android-x86/packages-apps-Taskbar.git] / app / src / main / java / com / farmerbb / taskbar / activity / HSLConfigActivity.java
1 /* Copyright 2020 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.content.pm.ResolveInfo;
24 import android.graphics.drawable.ColorDrawable;
25 import android.os.Build;
26 import android.os.Bundle;
27 import android.view.LayoutInflater;
28 import android.view.Menu;
29 import android.view.MenuItem;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.ArrayAdapter;
33 import android.widget.ListView;
34 import android.widget.TextView;
35
36 import androidx.appcompat.app.AppCompatActivity;
37 import androidx.core.content.ContextCompat;
38
39 import com.farmerbb.taskbar.R;
40 import com.farmerbb.taskbar.util.U;
41
42 import java.util.ArrayList;
43 import java.util.List;
44
45 import static com.farmerbb.taskbar.util.Constants.*;
46
47 public class HSLConfigActivity extends AppCompatActivity {
48
49     private boolean returnToSettings;
50
51     private final class LauncherListAdapter extends ArrayAdapter<String> {
52         private LauncherListAdapter(Context context, ArrayList<String> notes) {
53             super(context, R.layout.tb_hsl_row_layout, notes);
54         }
55
56         @Override
57         public View getView(int position, View convertView, ViewGroup parent) {
58             // Get the data item for this position
59             String launcher = getItem(position);
60             // Check if an existing view is being reused, otherwise inflate the view
61             if(convertView == null) {
62                 convertView = LayoutInflater.from(getContext()).inflate(R.layout.tb_hsl_row_layout, parent, false);
63             }
64             // Lookup view for data population
65             TextView launcherTitle = convertView.findViewById(R.id.launcherTitle);
66             // Populate the data into the template view using the data object
67             launcherTitle.setText(launcher);
68
69             // Return the completed view to render on screen
70             return convertView;
71         }
72     }
73
74     ArrayList<String> packageIds;
75     ArrayList<String> packageNames;
76
77     @Override
78     protected void onCreate(Bundle savedInstanceState) {
79         super.onCreate(savedInstanceState);
80
81         boolean isDarkTheme = U.isDarkTheme(this);
82         setTheme(isDarkTheme ? R.style.Taskbar_Dark : R.style.Taskbar);
83         setContentView(R.layout.tb_activity_hsl_config);
84
85         returnToSettings = getIntent().getBooleanExtra("return_to_settings", false);
86
87         SharedPreferences pref = U.getSharedPreferences(this);
88         pref.edit().putBoolean(PREF_DESKTOP_MODE, U.isDesktopModeSupported(this)).apply();
89
90         if(getSupportActionBar() == null) return;
91
92         if(returnToSettings) {
93             getSupportActionBar().setDisplayHomeAsUpEnabled(returnToSettings);
94             findViewById(R.id.space).setVisibility(View.GONE);
95         } else {
96             // Make action bar invisible
97             getSupportActionBar().setElevation(0);
98             getSupportActionBar().setBackgroundDrawable(new ColorDrawable(0));
99             getWindow().setStatusBarColor(ContextCompat.getColor(this, isDarkTheme
100                     ? R.color.tb_main_activity_background_dark
101                     : R.color.tb_main_activity_background));
102
103             if(!isDarkTheme && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
104                 View view = getWindow().getDecorView();
105                 view.setSystemUiVisibility(view.getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
106             }
107
108             setTitle(null);
109         }
110     }
111
112     @Override
113     protected void onStart() {
114         super.onStart();
115
116         packageIds = new ArrayList<>();
117         packageNames = new ArrayList<>();
118
119         // Get list of currently installed launchers
120         Intent homeIntent = new Intent(Intent.ACTION_MAIN);
121         homeIntent.addCategory(Intent.CATEGORY_HOME);
122         PackageManager manager = getPackageManager();
123         final List<ResolveInfo> listOfLaunchers = manager.queryIntentActivities(homeIntent, 0);
124
125         for(ResolveInfo launcher : listOfLaunchers) {
126             String string = launcher.activityInfo.packageName;
127
128             // Don't include the Settings dummy launcher, or Taskbar itself, on the list
129             if(!string.equals("com.android.settings")
130                     && !string.equals("com.android.tv.settings")
131                     && !string.equals("com.android.shell")
132                     && !string.startsWith("com.farmerbb.taskbar")) {
133                 if(string.equals("com.google.android.googlequicksearchbox")) {
134                     // Only add the Google App onto the list if Google Now Launcher is installed
135                     try {
136                         packageNames.add(manager.getApplicationInfo("com.google.android.launcher", 0).loadLabel(manager).toString());
137                         packageIds.add(string);
138                     } catch (PackageManager.NameNotFoundException e) { /* Gracefully fail */ }
139                 } else {
140                     packageNames.add(launcher.activityInfo.applicationInfo.loadLabel(manager).toString());
141                     packageIds.add(string);
142                 }
143             }
144         }
145
146         ListView listView = findViewById(R.id.listView);
147         TextView textView = findViewById(R.id.textView);
148
149         // Display the list of launchers
150         if(packageNames.size() > 0) {
151             textView.setText(R.string.tb_hsl_main_activity_text);
152
153             // Create the custom adapter to bind the array to the ListView
154             final LauncherListAdapter adapter = new LauncherListAdapter(this, packageNames);
155
156             // Display the ListView
157             listView.setAdapter(adapter);
158             listView.setClickable(true);
159             listView.setOnItemClickListener((arg0, arg1, position, arg3) -> {
160                 SharedPreferences pref = U.getSharedPreferences(HSLConfigActivity.this);
161                 SharedPreferences.Editor editor = pref.edit();
162                 editor.putString(PREF_HSL_ID, packageIds.get(position));
163                 editor.putString(PREF_HSL_NAME, packageNames.get(position));
164                 editor.apply();
165
166                 for(ResolveInfo launcher : listOfLaunchers) {
167                     if(packageIds.get(position).equals(launcher.activityInfo.packageName)) {
168                         if(!returnToSettings) {
169                             try {
170                                 startActivity(homeIntent);
171                             } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
172                         }
173
174                         finish();
175                     }
176                 }
177             });
178         } else
179             textView.setText(R.string.tb_hsl_no_launchers_found);
180     }
181
182     @Override
183     public boolean onCreateOptionsMenu(Menu menu) {
184         // Inflate action bar menu
185         if(!returnToSettings) getMenuInflater().inflate(R.menu.tb_hsl_config, menu);
186         return true;
187     }
188
189     @Override
190     public boolean onOptionsItemSelected(MenuItem item) {
191         // Handle presses on the action bar items
192         if(item.getItemId() == R.id.action_settings) {
193             Intent intent = new Intent(this, MainActivity.class);
194             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
195             startActivity(intent);
196         } else if(item.getItemId() == android.R.id.home)
197             onBackPressed();
198
199         return true;
200     }
201
202     @Override
203     public void onBackPressed() {
204         if(returnToSettings) super.onBackPressed();
205     }
206 }