blob: 42a134fdbb9477402c46b37ac5f5cb2d9d7dd3ed [file] [log] [blame]
Winson Chunga6945242014-01-08 14:04:34 -08001/*
2 * Copyright (C) 2008 The Android Open Source 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 com.android.launcher3;
18
19import android.accounts.Account;
20import android.accounts.AccountManager;
21import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
23import android.app.ActivityManager;
24import android.content.Context;
25import android.content.SharedPreferences;
26import android.os.Bundle;
27import android.os.UserManager;
28import android.view.LayoutInflater;
29import android.view.View;
30import android.view.ViewGroup;
31import android.view.accessibility.AccessibilityManager;
32import android.widget.TextView;
33
34class LauncherClings {
35 private static final String FIRST_RUN_CLING_DISMISSED_KEY = "cling_gel.first_run.dismissed";
36 private static final String MIGRATION_CLING_DISMISSED_KEY = "cling_gel.migration.dismissed";
37 private static final String MIGRATION_WORKSPACE_CLING_DISMISSED_KEY = "cling_gel.migration.dismissed";
38 private static final String WORKSPACE_CLING_DISMISSED_KEY = "cling_gel.workspace.dismissed";
39 private static final String FOLDER_CLING_DISMISSED_KEY = "cling_gel.folder.dismissed";
40
41 private static final boolean DISABLE_CLINGS = false;
42 private static final boolean DISABLE_CUSTOM_CLINGS = true;
43
44 private static final int SHOW_CLING_DURATION = 250;
45 private static final int DISMISS_CLING_DURATION = 200;
46
47 private Launcher mLauncher;
48 private LayoutInflater mInflater;
49 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
50 = new HideFromAccessibilityHelper();
51
52 /** Ctor */
53 public LauncherClings(Launcher launcher) {
54 mLauncher = launcher;
55 mInflater = mLauncher.getLayoutInflater();
56 }
57
58 /** Initializes a cling */
59 private Cling initCling(int clingId, int scrimId, boolean animate,
60 boolean dimNavBarVisibilty) {
61 Cling cling = (Cling) mLauncher.findViewById(clingId);
62 View scrim = null;
63 if (scrimId > 0) {
64 scrim = mLauncher.findViewById(R.id.cling_scrim);
65 }
66 if (cling != null) {
67 cling.init(mLauncher, scrim);
68 cling.show(animate, SHOW_CLING_DURATION);
69
70 if (dimNavBarVisibilty) {
71 cling.setSystemUiVisibility(cling.getSystemUiVisibility() |
72 View.SYSTEM_UI_FLAG_LOW_PROFILE);
73 }
74 }
75 return cling;
76 }
77
78 /** Returns whether the clings are enabled or should be shown */
79 private boolean isClingsEnabled() {
80 if (DISABLE_CLINGS) {
81 return false;
82 }
83
84 // For now, limit only to phones
85 LauncherAppState app = LauncherAppState.getInstance();
86 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
87 if (grid.isTablet()) {
88 return false;
89 }
90 if (grid.isLandscape) {
91 return false;
92 }
93
94 // disable clings when running in a test harness
95 if(ActivityManager.isRunningInTestHarness()) return false;
96
97 // Disable clings for accessibility when explore by touch is enabled
98 final AccessibilityManager a11yManager = (AccessibilityManager) mLauncher.getSystemService(
99 Launcher.ACCESSIBILITY_SERVICE);
100 if (a11yManager.isTouchExplorationEnabled()) {
101 return false;
102 }
103
104 // Restricted secondary users (child mode) will potentially have very few apps
105 // seeded when they start up for the first time. Clings won't work well with that
106 boolean supportsLimitedUsers =
107 android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
108 Account[] accounts = AccountManager.get(mLauncher).getAccounts();
109 if (supportsLimitedUsers && accounts.length == 0) {
110 UserManager um = (UserManager) mLauncher.getSystemService(Context.USER_SERVICE);
111 Bundle restrictions = um.getUserRestrictions();
112 if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
113 return false;
114 }
115 }
116 return true;
117 }
118
119 /** Returns whether the folder cling is visible. */
120 public boolean isFolderClingVisible() {
121 Cling cling = (Cling) mLauncher.findViewById(R.id.folder_cling);
122 if (cling != null) {
123 return cling.getVisibility() == View.VISIBLE;
124 }
125 return false;
126 }
127
128 private boolean skipCustomClingIfNoAccounts() {
129 Cling cling = (Cling) mLauncher.findViewById(R.id.workspace_cling);
130 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
131 if (customCling) {
132 AccountManager am = AccountManager.get(mLauncher);
133 if (am == null) return false;
134 Account[] accounts = am.getAccountsByType("com.google");
135 return accounts.length == 0;
136 }
137 return false;
138 }
139
140 /** Updates the first run cling custom content hint */
141 private void setCustomContentHintVisibility(Cling cling, String ccHintStr, boolean visible,
142 boolean animate) {
143 final TextView ccHint = (TextView) cling.findViewById(R.id.custom_content_hint);
144 if (ccHint != null) {
145 if (visible && !ccHintStr.isEmpty()) {
146 ccHint.setText(ccHintStr);
147 ccHint.setVisibility(View.VISIBLE);
148 if (animate) {
149 ccHint.setAlpha(0f);
150 ccHint.animate().alpha(1f)
151 .setDuration(SHOW_CLING_DURATION)
152 .start();
153 } else {
154 ccHint.setAlpha(1f);
155 }
156 } else {
157 if (animate) {
158 ccHint.animate().alpha(0f)
159 .setDuration(SHOW_CLING_DURATION)
160 .setListener(new AnimatorListenerAdapter() {
161 @Override
162 public void onAnimationEnd(Animator animation) {
163 ccHint.setVisibility(View.GONE);
164 }
165 })
166 .start();
167 } else {
168 ccHint.setAlpha(0f);
169 ccHint.setVisibility(View.GONE);
170 }
171 }
172 }
173 }
174
175 /** Updates the first run cling custom content hint */
176 public void updateCustomContentHintVisibility() {
177 Cling cling = (Cling) mLauncher.findViewById(R.id.first_run_cling);
178 String ccHintStr = mLauncher.getFirstRunCustomContentHint();
179
180 if (mLauncher.getWorkspace().hasCustomContent()) {
181 // Show the custom content hint if ccHintStr is not empty
182 if (cling != null) {
183 setCustomContentHintVisibility(cling, ccHintStr, true, true);
184 }
185 } else {
186 // Hide the custom content hint
187 if (cling != null) {
188 setCustomContentHintVisibility(cling, ccHintStr, false, true);
189 }
190 }
191 }
192
193 /** Updates the first run cling search bar hint. */
194 public void updateSearchBarHint(String hint) {
195 Cling cling = (Cling) mLauncher.findViewById(R.id.first_run_cling);
196 if (cling != null && cling.getVisibility() == View.VISIBLE && !hint.isEmpty()) {
197 TextView sbHint = (TextView) cling.findViewById(R.id.search_bar_hint);
198 sbHint.setText(hint);
199 sbHint.setVisibility(View.VISIBLE);
200 }
201 }
202
Winson Chunge43a1e72014-01-15 10:33:02 -0800203 public boolean shouldShowFirstRunOrMigrationClings() {
Winson Chunga6945242014-01-08 14:04:34 -0800204 SharedPreferences sharedPrefs = mLauncher.getSharedPrefs();
Winson Chunge43a1e72014-01-15 10:33:02 -0800205 return isClingsEnabled() &&
206 !sharedPrefs.getBoolean(FIRST_RUN_CLING_DISMISSED_KEY, false) &&
207 !sharedPrefs.getBoolean(MIGRATION_CLING_DISMISSED_KEY, false);
208 }
Winson Chunga6945242014-01-08 14:04:34 -0800209
Winson Chunge43a1e72014-01-15 10:33:02 -0800210 public void removeFirstRunAndMigrationClings() {
211 removeCling(R.id.first_run_cling);
212 removeCling(R.id.migration_cling);
213 }
Winson Chunga6945242014-01-08 14:04:34 -0800214
Winson Chunge43a1e72014-01-15 10:33:02 -0800215 /**
216 * Shows the first run cling.
217 *
218 * This flow is mutually exclusive with showMigrationCling, and only runs if this Launcher
219 * package was preinstalled or there is no db to migrate from.
220 */
221 public void showFirstRunCling() {
222 if (!skipCustomClingIfNoAccounts()) {
223 SharedPreferences sharedPrefs = mLauncher.getSharedPrefs();
Winson Chunga6945242014-01-08 14:04:34 -0800224 // If we're not using the default workspace layout, replace workspace cling
225 // with a custom workspace cling (usually specified in an overlay)
226 // For now, only do this on tablets
227 if (!DISABLE_CUSTOM_CLINGS) {
228 if (sharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
229 mLauncher.getResources().getBoolean(R.bool.config_useCustomClings)) {
230 // Use a custom cling
231 View cling = mLauncher.findViewById(R.id.workspace_cling);
232 ViewGroup clingParent = (ViewGroup) cling.getParent();
233 int clingIndex = clingParent.indexOfChild(cling);
234 clingParent.removeViewAt(clingIndex);
235 View customCling = mInflater.inflate(R.layout.custom_workspace_cling,
236 clingParent, false);
237 clingParent.addView(customCling, clingIndex);
238 customCling.setId(R.id.workspace_cling);
239 }
240 }
241 Cling cling = (Cling) mLauncher.findViewById(R.id.first_run_cling);
242 if (cling != null) {
243 String sbHintStr = mLauncher.getFirstRunClingSearchBarHint();
244 String ccHintStr = mLauncher.getFirstRunCustomContentHint();
245 if (!sbHintStr.isEmpty()) {
246 TextView sbHint = (TextView) cling.findViewById(R.id.search_bar_hint);
247 sbHint.setText(sbHintStr);
248 sbHint.setVisibility(View.VISIBLE);
249 }
250 setCustomContentHintVisibility(cling, ccHintStr, true, false);
251 }
252 initCling(R.id.first_run_cling, 0, false, true);
253 } else {
Winson Chunge43a1e72014-01-15 10:33:02 -0800254 removeFirstRunAndMigrationClings();
Winson Chunga6945242014-01-08 14:04:34 -0800255 }
256 }
257
Winson Chunge43a1e72014-01-15 10:33:02 -0800258 /**
259 * Shows the migration cling.
260 *
261 * This flow is mutually exclusive with showFirstRunCling, and only runs if this Launcher
262 * package was not preinstalled and there exists a db to migrate from.
263 */
Winson Chunga6945242014-01-08 14:04:34 -0800264 public void showMigrationCling() {
Winson Chunge43a1e72014-01-15 10:33:02 -0800265 mLauncher.hideWorkspaceSearchAndHotseat();
Winson Chunga6945242014-01-08 14:04:34 -0800266
Winson Chunge43a1e72014-01-15 10:33:02 -0800267 Cling c = initCling(R.id.migration_cling, 0, false, true);
268 c.bringScrimToFront();
269 c.bringToFront();
Winson Chunga6945242014-01-08 14:04:34 -0800270 }
271
272 public void showMigrationWorkspaceCling() {
273 // Enable the clings only if they have not been dismissed before
274 if (isClingsEnabled() && !mLauncher.getSharedPrefs().getBoolean(
275 MIGRATION_WORKSPACE_CLING_DISMISSED_KEY, false)) {
276 Cling c = initCling(R.id.migration_workspace_cling, 0, false, true);
277 c.updateMigrationWorkspaceBubblePosition();
278 c.bringScrimToFront();
279 c.bringToFront();
280 } else {
281 removeCling(R.id.migration_workspace_cling);
282 }
283 }
284
285 public void showWorkspaceCling() {
286 // Enable the clings only if they have not been dismissed before
287 if (isClingsEnabled() && !mLauncher.getSharedPrefs().getBoolean(
288 WORKSPACE_CLING_DISMISSED_KEY, false)) {
289 Cling c = initCling(R.id.workspace_cling, 0, false, true);
290
291 // Set the focused hotseat app if there is one
292 c.setFocusedHotseatApp(mLauncher.getFirstRunFocusedHotseatAppDrawableId(),
293 mLauncher.getFirstRunFocusedHotseatAppRank(),
294 mLauncher.getFirstRunFocusedHotseatAppComponentName(),
295 mLauncher.getFirstRunFocusedHotseatAppBubbleTitle(),
296 mLauncher.getFirstRunFocusedHotseatAppBubbleDescription());
297 } else {
298 removeCling(R.id.workspace_cling);
299 }
300 }
301 public Cling showFoldersCling() {
302 SharedPreferences sharedPrefs = mLauncher.getSharedPrefs();
303 // Enable the clings only if they have not been dismissed before
304 if (isClingsEnabled() &&
305 !sharedPrefs.getBoolean(FOLDER_CLING_DISMISSED_KEY, false) &&
306 !sharedPrefs.getBoolean(Launcher.USER_HAS_MIGRATED, false)) {
307 Cling cling = initCling(R.id.folder_cling, R.id.cling_scrim,
308 true, true);
309 return cling;
310 } else {
311 removeCling(R.id.folder_cling);
312 return null;
313 }
314 }
315
Winson Chunga6945242014-01-08 14:04:34 -0800316 /** Removes the cling outright from the DragLayer */
317 private void removeCling(int id) {
318 final View cling = mLauncher.findViewById(id);
319 if (cling != null) {
320 final ViewGroup parent = (ViewGroup) cling.getParent();
321 parent.post(new Runnable() {
322 @Override
323 public void run() {
324 parent.removeView(cling);
325 }
326 });
327 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mLauncher.getDragLayer());
328 }
329 }
330
331 /** Hides the specified Cling */
332 private void dismissCling(final Cling cling, final Runnable postAnimationCb,
333 final String flag, int duration, boolean restoreNavBarVisibilty) {
334 // To catch cases where siblings of top-level views are made invisible, just check whether
335 // the cling is directly set to GONE before dismissing it.
336 if (cling != null && cling.getVisibility() != View.GONE) {
337 final Runnable cleanUpClingCb = new Runnable() {
338 public void run() {
339 cling.cleanup();
340 SharedPreferences.Editor editor = mLauncher.getSharedPrefs().edit();
341 editor.putBoolean(flag, true);
342 editor.apply();
343 if (postAnimationCb != null) {
344 postAnimationCb.run();
345 }
346 }
347 };
348 if (duration <= 0) {
349 cleanUpClingCb.run();
350 } else {
351 cling.hide(duration, cleanUpClingCb);
352 }
353 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mLauncher.getDragLayer());
354
355 if (restoreNavBarVisibilty) {
356 cling.setSystemUiVisibility(cling.getSystemUiVisibility() &
357 ~View.SYSTEM_UI_FLAG_LOW_PROFILE);
358 }
359 }
360 }
361
362 public void dismissFirstRunCling(View v) {
363 Cling cling = (Cling) mLauncher.findViewById(R.id.first_run_cling);
364 Runnable cb = new Runnable() {
365 public void run() {
366 // Show the workspace cling next
367 showWorkspaceCling();
368 }
369 };
370 dismissCling(cling, cb, FIRST_RUN_CLING_DISMISSED_KEY,
371 DISMISS_CLING_DURATION, false);
372
373 // Fade out the search bar for the workspace cling coming up
374 mLauncher.getSearchBar().hideSearchBar(true);
375 }
376
377 private void dismissMigrationCling() {
378 mLauncher.showWorkspaceSearchAndHotseat();
379 Runnable dismissCb = new Runnable() {
380 public void run() {
381 Cling cling = (Cling) mLauncher.findViewById(R.id.migration_cling);
382 Runnable cb = new Runnable() {
383 public void run() {
384 // Show the migration workspace cling next
385 showMigrationWorkspaceCling();
386 }
387 };
Winson Chung285b6e12014-01-14 10:30:27 -0800388 dismissCling(cling, cb, MIGRATION_CLING_DISMISSED_KEY,
Winson Chunga6945242014-01-08 14:04:34 -0800389 DISMISS_CLING_DURATION, true);
390 }
391 };
392 mLauncher.getWorkspace().post(dismissCb);
393 }
394
Winson Chung285b6e12014-01-14 10:30:27 -0800395 private void dismissAnyWorkspaceCling(Cling cling, String key, View v) {
Winson Chunga6945242014-01-08 14:04:34 -0800396 Runnable cb = null;
397 if (v == null) {
398 cb = new Runnable() {
399 public void run() {
400 mLauncher.getWorkspace().enterOverviewMode();
401 }
402 };
403 }
Winson Chung285b6e12014-01-14 10:30:27 -0800404 dismissCling(cling, cb, key, DISMISS_CLING_DURATION, true);
Winson Chunga6945242014-01-08 14:04:34 -0800405
406 // Fade in the search bar
407 mLauncher.getSearchBar().showSearchBar(true);
408 }
409
410 public void dismissMigrationClingCopyApps(View v) {
411 // Copy the shortcuts from the old database
412 LauncherModel model = mLauncher.getModel();
Winson Chung5317c2b2014-01-10 16:46:15 -0800413 model.resetLoadedState(false, true);
414 model.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
415 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
416 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Winson Chunga6945242014-01-08 14:04:34 -0800417
418 // Set the flag to skip the folder cling
419 String spKey = LauncherAppState.getSharedPreferencesKey();
420 SharedPreferences sp = mLauncher.getSharedPreferences(spKey, Context.MODE_PRIVATE);
421 SharedPreferences.Editor editor = sp.edit();
422 editor.putBoolean(Launcher.USER_HAS_MIGRATED, true);
423 editor.apply();
424
425 // Disable the migration cling
426 dismissMigrationCling();
427 }
428
429 public void dismissMigrationClingUseDefault(View v) {
430 // Clear the workspace
431 LauncherModel model = mLauncher.getModel();
Winson Chung5317c2b2014-01-10 16:46:15 -0800432 model.resetLoadedState(false, true);
433 model.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
434 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
Winson Chunga6945242014-01-08 14:04:34 -0800435
436 // Disable the migration cling
437 dismissMigrationCling();
438 }
439
440 public void dismissMigrationWorkspaceCling(View v) {
441 Cling cling = (Cling) mLauncher.findViewById(R.id.migration_workspace_cling);
Winson Chung285b6e12014-01-14 10:30:27 -0800442 dismissAnyWorkspaceCling(cling, MIGRATION_WORKSPACE_CLING_DISMISSED_KEY, v);
Winson Chunga6945242014-01-08 14:04:34 -0800443 }
444
445 public void dismissWorkspaceCling(View v) {
446 Cling cling = (Cling) mLauncher.findViewById(R.id.workspace_cling);
Winson Chung285b6e12014-01-14 10:30:27 -0800447 dismissAnyWorkspaceCling(cling, WORKSPACE_CLING_DISMISSED_KEY, v);
Winson Chunga6945242014-01-08 14:04:34 -0800448 }
449
450 public void dismissFolderCling(View v) {
451 Cling cling = (Cling) mLauncher.findViewById(R.id.folder_cling);
452 dismissCling(cling, null, FOLDER_CLING_DISMISSED_KEY,
453 DISMISS_CLING_DURATION, true);
454 }
455}