1 /* Copyright 2020 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.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;
36 import androidx.appcompat.app.AppCompatActivity;
37 import androidx.core.content.ContextCompat;
39 import com.farmerbb.taskbar.R;
40 import com.farmerbb.taskbar.util.U;
42 import java.util.ArrayList;
43 import java.util.List;
45 import static com.farmerbb.taskbar.util.Constants.*;
47 public class HSLConfigActivity extends AppCompatActivity {
49 private boolean returnToSettings;
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);
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);
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);
69 // Return the completed view to render on screen
74 ArrayList<String> packageIds;
75 ArrayList<String> packageNames;
78 protected void onCreate(Bundle savedInstanceState) {
79 super.onCreate(savedInstanceState);
81 boolean isDarkTheme = U.isDarkTheme(this);
82 setTheme(isDarkTheme ? R.style.Taskbar_Dark : R.style.Taskbar);
83 setContentView(R.layout.tb_activity_hsl_config);
85 returnToSettings = getIntent().getBooleanExtra("return_to_settings", false);
87 SharedPreferences pref = U.getSharedPreferences(this);
88 pref.edit().putBoolean(PREF_DESKTOP_MODE, U.isDesktopModeSupported(this)).apply();
90 if(getSupportActionBar() == null) return;
92 if(returnToSettings) {
93 getSupportActionBar().setDisplayHomeAsUpEnabled(returnToSettings);
94 findViewById(R.id.space).setVisibility(View.GONE);
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));
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);
113 protected void onStart() {
116 packageIds = new ArrayList<>();
117 packageNames = new ArrayList<>();
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);
125 for(ResolveInfo launcher : listOfLaunchers) {
126 String string = launcher.activityInfo.packageName;
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
136 packageNames.add(manager.getApplicationInfo("com.google.android.launcher", 0).loadLabel(manager).toString());
137 packageIds.add(string);
138 } catch (PackageManager.NameNotFoundException e) { /* Gracefully fail */ }
140 packageNames.add(launcher.activityInfo.applicationInfo.loadLabel(manager).toString());
141 packageIds.add(string);
146 ListView listView = findViewById(R.id.listView);
147 TextView textView = findViewById(R.id.textView);
149 // Display the list of launchers
150 if(packageNames.size() > 0) {
151 textView.setText(R.string.tb_hsl_main_activity_text);
153 // Create the custom adapter to bind the array to the ListView
154 final LauncherListAdapter adapter = new LauncherListAdapter(this, packageNames);
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));
166 for(ResolveInfo launcher : listOfLaunchers) {
167 if(packageIds.get(position).equals(launcher.activityInfo.packageName)) {
168 if(!returnToSettings) {
170 startActivity(homeIntent);
171 } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
179 textView.setText(R.string.tb_hsl_no_launchers_found);
183 public boolean onCreateOptionsMenu(Menu menu) {
184 // Inflate action bar menu
185 if(!returnToSettings) getMenuInflater().inflate(R.menu.tb_hsl_config, menu);
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)
203 public void onBackPressed() {
204 if(returnToSettings) super.onBackPressed();