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.fragment;
18 import android.annotation.TargetApi;
19 import android.content.ActivityNotFoundException;
20 import android.content.ComponentName;
21 import android.content.Intent;
22 import android.content.SharedPreferences;
23 import android.content.pm.PackageManager;
24 import android.os.Bundle;
25 import android.preference.CheckBoxPreference;
26 import android.preference.Preference;
27 import android.provider.Settings;
29 import androidx.appcompat.app.ActionBar;
30 import androidx.appcompat.app.AppCompatActivity;
32 import com.farmerbb.taskbar.R;
33 import com.farmerbb.taskbar.activity.HSLActivity;
34 import com.farmerbb.taskbar.activity.HSLConfigActivity;
35 import com.farmerbb.taskbar.activity.SecondaryHomeActivity;
36 import com.farmerbb.taskbar.util.U;
38 public class DesktopModeFragment extends SettingsFragment {
40 public static boolean isConfiguringHomeApp;
43 public void onCreate(Bundle savedInstanceState) {
44 finishedLoadingPrefs = false;
46 super.onCreate(savedInstanceState);
49 addPreferencesFromResource(R.xml.tb_pref_desktop_mode);
51 // Set OnClickListeners for certain preferences
52 findPreference("desktop_mode").setOnPreferenceClickListener(this);
53 findPreference("set_launcher_default").setOnPreferenceClickListener(this);
54 findPreference("primary_launcher").setOnPreferenceClickListener(this);
56 SharedPreferences pref = U.getSharedPreferences(getActivity());
57 if(pref.getBoolean("launcher", false))
58 findPreference("desktop_mode").setEnabled(false);
60 bindPreferenceSummaryToValue(findPreference("desktop_mode"));
62 bindPreferenceSummaryToValue(findPreference("display_density"));
64 boolean writeSecureSettings = U.hasWriteSecureSettingsPermission(getActivity());
65 findPreference("display_density").setEnabled(writeSecureSettings);
66 findPreference("auto_hide_navbar").setEnabled(writeSecureSettings);
68 finishedLoadingPrefs = true;
72 public void onActivityCreated(Bundle savedInstanceState) {
73 super.onActivityCreated(savedInstanceState);
75 AppCompatActivity activity = (AppCompatActivity) getActivity();
76 activity.setTitle(R.string.tb_pref_header_desktop_mode);
77 ActionBar actionBar = activity.getSupportActionBar();
79 actionBar.setDisplayHomeAsUpEnabled(true);
83 public void onResume() {
85 isConfiguringHomeApp = false;
87 Preference primaryLauncherPref = findPreference("primary_launcher");
88 if(primaryLauncherPref != null) {
89 SharedPreferences pref = U.getSharedPreferences(getActivity());
90 String primaryLauncherName = pref.getString("hsl_name", "null");
91 String primaryLauncherPackage = pref.getString("hsl_id", "null");
93 boolean primaryLauncherValid = true;
95 getActivity().getPackageManager().getPackageInfo(primaryLauncherPackage, 0);
96 } catch (PackageManager.NameNotFoundException e) {
97 primaryLauncherValid = false;
100 primaryLauncherPref.setSummary(primaryLauncherValid
101 ? primaryLauncherName
102 : getString(R.string.tb_icon_pack_none)
109 public boolean onPreferenceClick(final Preference p) {
112 ComponentName component = new ComponentName(getActivity(), SecondaryHomeActivity.class);
113 getActivity().getPackageManager().setComponentEnabledSetting(component,
114 ((CheckBoxPreference) p).isChecked()
115 ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
116 : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
117 PackageManager.DONT_KILL_APP);
119 ComponentName component2 = new ComponentName(getActivity(), HSLActivity.class);
120 getActivity().getPackageManager().setComponentEnabledSetting(component2,
121 ((CheckBoxPreference) p).isChecked()
122 ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
123 : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
124 PackageManager.DONT_KILL_APP);
127 case "set_launcher_default":
129 startActivity(new Intent(Settings.ACTION_HOME_SETTINGS));
130 isConfiguringHomeApp = true;
131 } catch (ActivityNotFoundException e) {
132 U.showToastLong(getActivity(), R.string.tb_unable_to_set_default_home);
136 case "primary_launcher":
137 Intent intent = new Intent(getActivity(), HSLConfigActivity.class);
138 intent.putExtra("return_to_settings", true);
139 startActivity(intent);
144 return super.onPreferenceClick(p);