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 {
41 public void onCreate(Bundle savedInstanceState) {
42 finishedLoadingPrefs = false;
44 super.onCreate(savedInstanceState);
47 addPreferencesFromResource(R.xml.tb_pref_desktop_mode);
49 // Set OnClickListeners for certain preferences
50 findPreference("desktop_mode").setOnPreferenceClickListener(this);
51 findPreference("set_launcher_default").setOnPreferenceClickListener(this);
52 findPreference("primary_launcher").setOnPreferenceClickListener(this);
54 SharedPreferences pref = U.getSharedPreferences(getActivity());
55 if(pref.getBoolean("launcher", false))
56 findPreference("desktop_mode").setEnabled(false);
58 bindPreferenceSummaryToValue(findPreference("desktop_mode"));
60 bindPreferenceSummaryToValue(findPreference("display_density"));
62 boolean writeSecureSettings = U.hasWriteSecureSettingsPermission(getActivity());
63 findPreference("display_density").setEnabled(writeSecureSettings);
64 findPreference("auto_hide_navbar").setEnabled(writeSecureSettings);
66 finishedLoadingPrefs = true;
70 public void onActivityCreated(Bundle savedInstanceState) {
71 super.onActivityCreated(savedInstanceState);
73 AppCompatActivity activity = (AppCompatActivity) getActivity();
74 activity.setTitle(R.string.tb_pref_header_desktop_mode);
75 ActionBar actionBar = activity.getSupportActionBar();
77 actionBar.setDisplayHomeAsUpEnabled(true);
81 public void onResume() {
84 Preference primaryLauncherPref = findPreference("primary_launcher");
85 if(primaryLauncherPref != null) {
86 SharedPreferences pref = U.getSharedPreferences(getActivity());
87 String primaryLauncherName = pref.getString("hsl_name", "null");
88 String primaryLauncherPackage = pref.getString("hsl_id", "null");
90 boolean primaryLauncherValid = true;
92 getActivity().getPackageManager().getPackageInfo(primaryLauncherPackage, 0);
93 } catch (PackageManager.NameNotFoundException e) {
94 primaryLauncherValid = false;
97 primaryLauncherPref.setSummary(primaryLauncherValid
99 : getString(R.string.tb_icon_pack_none)
106 public boolean onPreferenceClick(final Preference p) {
109 ComponentName component = new ComponentName(getActivity(), SecondaryHomeActivity.class);
110 getActivity().getPackageManager().setComponentEnabledSetting(component,
111 ((CheckBoxPreference) p).isChecked()
112 ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
113 : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
114 PackageManager.DONT_KILL_APP);
116 ComponentName component2 = new ComponentName(getActivity(), HSLActivity.class);
117 getActivity().getPackageManager().setComponentEnabledSetting(component2,
118 ((CheckBoxPreference) p).isChecked()
119 ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
120 : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
121 PackageManager.DONT_KILL_APP);
124 case "set_launcher_default":
126 startActivity(new Intent(Settings.ACTION_HOME_SETTINGS));
127 } catch (ActivityNotFoundException e) {
128 U.showToastLong(getActivity(), R.string.tb_unable_to_set_default_home);
132 case "primary_launcher":
133 Intent intent = new Intent(getActivity(), HSLConfigActivity.class);
134 intent.putExtra("return_to_settings", true);
135 startActivity(intent);
140 return super.onPreferenceClick(p);