OSDN Git Service

Improvements for setting default home app
[android-x86/packages-apps-Taskbar.git] / app / src / main / java / com / farmerbb / taskbar / fragment / DesktopModeFragment.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.fragment;
17
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;
28
29 import androidx.appcompat.app.ActionBar;
30 import androidx.appcompat.app.AppCompatActivity;
31
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;
37
38 public class DesktopModeFragment extends SettingsFragment {
39
40     public static boolean isConfiguringHomeApp;
41
42     @Override
43     public void onCreate(Bundle savedInstanceState) {
44         finishedLoadingPrefs = false;
45
46         super.onCreate(savedInstanceState);
47
48         // Add preferences
49         addPreferencesFromResource(R.xml.tb_pref_desktop_mode);
50
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);
55
56         SharedPreferences pref = U.getSharedPreferences(getActivity());
57         if(pref.getBoolean("launcher", false))
58             findPreference("desktop_mode").setEnabled(false);
59         else
60             bindPreferenceSummaryToValue(findPreference("desktop_mode"));
61
62         bindPreferenceSummaryToValue(findPreference("display_density"));
63
64         boolean writeSecureSettings = U.hasWriteSecureSettingsPermission(getActivity());
65         findPreference("display_density").setEnabled(writeSecureSettings);
66         findPreference("auto_hide_navbar").setEnabled(writeSecureSettings);
67
68         finishedLoadingPrefs = true;
69     }
70
71     @Override
72     public void onActivityCreated(Bundle savedInstanceState) {
73         super.onActivityCreated(savedInstanceState);
74
75         AppCompatActivity activity = (AppCompatActivity) getActivity();
76         activity.setTitle(R.string.tb_pref_header_desktop_mode);
77         ActionBar actionBar = activity.getSupportActionBar();
78         if(actionBar != null)
79             actionBar.setDisplayHomeAsUpEnabled(true);
80     }
81
82     @Override
83     public void onResume() {
84         super.onResume();
85         isConfiguringHomeApp = false;
86
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");
92
93             boolean primaryLauncherValid = true;
94             try {
95                 getActivity().getPackageManager().getPackageInfo(primaryLauncherPackage, 0);
96             } catch (PackageManager.NameNotFoundException e) {
97                 primaryLauncherValid = false;
98             }
99
100             primaryLauncherPref.setSummary(primaryLauncherValid
101                     ? primaryLauncherName
102                     : getString(R.string.tb_icon_pack_none)
103             );
104         }
105     }
106
107     @TargetApi(29)
108     @Override
109     public boolean onPreferenceClick(final Preference p) {
110         switch(p.getKey()) {
111             case "desktop_mode":
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);
118
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);
125
126                break;
127             case "set_launcher_default":
128                 try {
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);
133                 }
134
135                 break;
136             case "primary_launcher":
137                 Intent intent = new Intent(getActivity(), HSLConfigActivity.class);
138                 intent.putExtra("return_to_settings", true);
139                 startActivity(intent);
140
141                 break;
142         }
143
144         return super.onPreferenceClick(p);
145     }
146 }