blob: c5f6195ab6a03e627eb549fd30beada637ea532e [file] [log] [blame]
Timi Rautamäkie83f9e12022-03-16 13:21:30 +00001/*
2 * Copyright (C) 2022 The LineageOS Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.lineageos.setupwizard;
18
19import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_2BUTTON_OVERLAY;
20import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_3BUTTON_OVERLAY;
21import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_GESTURAL_OVERLAY;
22
23import static org.lineageos.internal.util.DeviceKeysConstants.KEY_MASK_APP_SWITCH;
24import static org.lineageos.setupwizard.SetupWizardApp.DISABLE_NAV_KEYS;
25import static org.lineageos.setupwizard.SetupWizardApp.NAVIGATION_OPTION_KEY;
26
27import android.animation.Animator;
28import android.animation.AnimatorListenerAdapter;
29import android.app.Activity;
30import android.content.Intent;
31import android.os.Bundle;
32import android.os.UserHandle;
33import android.content.Context;
34import android.view.View;
35import android.widget.CheckBox;
36import android.widget.RadioButton;
37import android.widget.RadioGroup;
38import android.widget.RadioGroup.OnCheckedChangeListener;
39
40import com.airbnb.lottie.LottieAnimationView;
41import com.google.android.setupcompat.util.WizardManagerHelper;
42
43import lineageos.providers.LineageSettings;
44
45import org.lineageos.setupwizard.util.SetupWizardUtils;
46
47public class NavigationSettingsActivity extends BaseSetupWizardActivity {
48
49 public static final String TAG = NavigationSettingsActivity.class.getSimpleName();
50
51 private SetupWizardApp mSetupWizardApp;
52
53 private String mSelection = NAV_BAR_MODE_GESTURAL_OVERLAY;
54
55 private CheckBox mHideGesturalHint;
56
57 @Override
58 protected void onCreate(Bundle savedInstanceState) {
59 super.onCreate(savedInstanceState);
60
61 mSetupWizardApp = (SetupWizardApp) getApplication();
62 boolean navBarEnabled = false;
63 if (mSetupWizardApp.getSettingsBundle().containsKey(DISABLE_NAV_KEYS)) {
64 navBarEnabled = mSetupWizardApp.getSettingsBundle().getBoolean(DISABLE_NAV_KEYS);
65 }
66
67 int deviceKeys = getResources().getInteger(
68 org.lineageos.platform.internal.R.integer.config_deviceHardwareKeys);
69 boolean hasHomeKey = (deviceKeys & KEY_MASK_APP_SWITCH) != 0;
70
71 getGlifLayout().setDescriptionText(getString(R.string.navigation_summary));
72 setNextText(R.string.next);
73
74 int available = 3;
75 // Hide unavailable navigation modes
76 if (!isOverlayPackageAvailable(this, NAV_BAR_MODE_GESTURAL_OVERLAY)) {
77 findViewById(R.id.radio_gesture).setVisibility(View.GONE);
78 ((RadioButton) findViewById(R.id.radio_sw_keys)).setChecked(true);
79 available--;
80 }
81
82 if (!isOverlayPackageAvailable(this, NAV_BAR_MODE_2BUTTON_OVERLAY)) {
83 findViewById(R.id.radio_two_button).setVisibility(View.GONE);
84 available--;
85 }
86
87 if (!isOverlayPackageAvailable(this, NAV_BAR_MODE_3BUTTON_OVERLAY)) {
88 findViewById(R.id.radio_sw_keys).setVisibility(View.GONE);
89 available--;
90 }
91
92
93 // Hide this page if the device has hardware keys but didn't enable navbar
94 // or if there's <= 1 available navigation modes
95 if (!navBarEnabled && hasHomeKey || available <= 1) {
96 mSetupWizardApp.getSettingsBundle().putString(NAVIGATION_OPTION_KEY,
97 NAV_BAR_MODE_3BUTTON_OVERLAY);
98 Intent intent = WizardManagerHelper.getNextIntent(getIntent(), Activity.RESULT_OK);
99 finishAction(RESULT_OK, intent);
100 }
101
102 final LottieAnimationView navigationIllustration =
103 findViewById(R.id.navigation_illustration);
104 final RadioGroup radioGroup = findViewById(R.id.navigation_radio_group);
105 mHideGesturalHint = findViewById(R.id.hide_navigation_hint);
106 radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
107 @Override
108 public void onCheckedChanged(RadioGroup group, int checkedId) {
109 switch (checkedId) {
110 case R.id.radio_gesture:
111 mSelection = NAV_BAR_MODE_GESTURAL_OVERLAY;
112 navigationIllustration
113 .setAnimation(R.raw.lottie_system_nav_fully_gestural);
114 revealHintCheckbox();
115 break;
116 case R.id.radio_two_button:
117 mSelection = NAV_BAR_MODE_2BUTTON_OVERLAY;
118 navigationIllustration.setAnimation(R.raw.lottie_system_nav_2_button);
119 hideHintCheckBox();
120 break;
121 case R.id.radio_sw_keys:
122 mSelection = NAV_BAR_MODE_3BUTTON_OVERLAY;
123 navigationIllustration.setAnimation(R.raw.lottie_system_nav_3_button);
124 hideHintCheckBox();
125 break;
126 }
127
128 navigationIllustration.playAnimation();
129 }
130 });
131 }
132
133 private void revealHintCheckbox() {
134 if (mHideGesturalHint.getVisibility() == View.VISIBLE) {
135 return;
136 }
137
138 mHideGesturalHint.setVisibility(View.VISIBLE);
139 mHideGesturalHint.setAlpha(0.0f);
140 mHideGesturalHint.animate()
141 .translationY(0)
142 .alpha(1.0f)
143 .setListener(null);
144 }
145
146 private void hideHintCheckBox() {
147 if (mHideGesturalHint.getVisibility() == View.INVISIBLE) {
148 return;
149 }
150
151 mHideGesturalHint.animate()
152 .translationY(-mHideGesturalHint.getHeight())
153 .alpha(0.0f)
154 .setListener(new AnimatorListenerAdapter() {
155 @Override
156 public void onAnimationEnd(Animator animation) {
157 super.onAnimationEnd(animation);
158 mHideGesturalHint.setVisibility(View.INVISIBLE);
159 }
160 });
161 }
162
163 @Override
164 protected void onNextPressed() {
165 mSetupWizardApp.getSettingsBundle().putString(NAVIGATION_OPTION_KEY, mSelection);
166 boolean hideHint = mHideGesturalHint.isChecked();
167 LineageSettings.System.putIntForUser(getContentResolver(),
168 LineageSettings.System.NAVIGATION_BAR_HINT, hideHint ? 0 : 1,
169 UserHandle.USER_CURRENT);
170 Intent intent = WizardManagerHelper.getNextIntent(getIntent(), Activity.RESULT_OK);
171 nextAction(NEXT_REQUEST, intent);
172 }
173
174 @Override
175 protected int getLayoutResId() {
176 return R.layout.setup_navigation;
177 }
178
179 @Override
180 protected int getTitleResId() {
181 return R.string.setup_navigation;
182 }
183
184 @Override
185 protected int getIconResId() {
186 return R.drawable.ic_navigation;
187 }
188
189 private static boolean isOverlayPackageAvailable(Context context, String overlayPackage) {
190 try {
191 return context.getPackageManager().getPackageInfo(overlayPackage, 0) != null;
192 } catch (Exception e) {
193 // Not found, just return unavailable
194 return false;
195 }
196 }
197}