OSDN Git Service

Continue work on standalone Android 10 Desktop Mode support
[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.Intent;
21 import android.content.SharedPreferences;
22 import android.os.Bundle;
23 import android.preference.Preference;
24
25 import androidx.appcompat.app.ActionBar;
26 import androidx.appcompat.app.AppCompatActivity;
27
28 import com.farmerbb.taskbar.R;
29 import com.farmerbb.taskbar.util.U;
30
31 public class DesktopModeFragment extends SettingsFragment {
32
33     @Override
34     public void onCreate(Bundle savedInstanceState) {
35         finishedLoadingPrefs = false;
36
37         super.onCreate(savedInstanceState);
38
39         // Add preferences
40         addPreferencesFromResource(R.xml.tb_pref_desktop_mode);
41
42         // Set OnClickListeners for certain preferences
43         findPreference("set_as_default").setOnPreferenceClickListener(this);
44
45         SharedPreferences pref = U.getSharedPreferences(getActivity());
46         if(pref.getBoolean("launcher", false))
47             findPreference("desktop_mode").setEnabled(false);
48         else
49             bindPreferenceSummaryToValue(findPreference("desktop_mode"));
50
51         finishedLoadingPrefs = true;
52     }
53
54     @Override
55     public void onActivityCreated(Bundle savedInstanceState) {
56         super.onActivityCreated(savedInstanceState);
57
58         AppCompatActivity activity = (AppCompatActivity) getActivity();
59         activity.setTitle(R.string.tb_pref_header_desktop_mode);
60         ActionBar actionBar = activity.getSupportActionBar();
61         if(actionBar != null)
62             actionBar.setDisplayHomeAsUpEnabled(true);
63     }
64
65     @TargetApi(29)
66     @Override
67     public boolean onPreferenceClick(final Preference p) {
68         final SharedPreferences pref = U.getSharedPreferences(getActivity());
69
70         switch(p.getKey()) {
71             case "set_as_default":
72                 Intent intent = new Intent();
73                 intent.setAction(Intent.ACTION_MAIN);
74                 intent.addCategory(Intent.CATEGORY_SECONDARY_HOME);
75
76                 try {
77                     startActivity(intent);
78                 } catch (Exception e) { /* Gracefully fail */ }
79
80                 break;
81         }
82
83         return super.onPreferenceClick(p);
84     }
85 }