blob: 94b062ddc45e4ec1c4d413267a38d4beb4bf8bde [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;
Winson Chung205cd772014-01-15 14:31:59 -080026import android.graphics.Rect;
Winson Chunga6945242014-01-08 14:04:34 -080027import android.os.Bundle;
28import android.os.UserManager;
29import android.view.LayoutInflater;
30import android.view.View;
31import android.view.ViewGroup;
32import android.view.accessibility.AccessibilityManager;
33import android.widget.TextView;
34
35class LauncherClings {
36 private static final String FIRST_RUN_CLING_DISMISSED_KEY = "cling_gel.first_run.dismissed";
37 private static final String MIGRATION_CLING_DISMISSED_KEY = "cling_gel.migration.dismissed";
Winson Chung205cd772014-01-15 14:31:59 -080038 private static final String MIGRATION_WORKSPACE_CLING_DISMISSED_KEY =
39 "cling_gel.migration_workspace.dismissed";
Winson Chunga6945242014-01-08 14:04:34 -080040 private static final String WORKSPACE_CLING_DISMISSED_KEY = "cling_gel.workspace.dismissed";
41 private static final String FOLDER_CLING_DISMISSED_KEY = "cling_gel.folder.dismissed";
42
43 private static final boolean DISABLE_CLINGS = false;
44 private static final boolean DISABLE_CUSTOM_CLINGS = true;
45
46 private static final int SHOW_CLING_DURATION = 250;
47 private static final int DISMISS_CLING_DURATION = 200;
48
49 private Launcher mLauncher;
50 private LayoutInflater mInflater;
51 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
52 = new HideFromAccessibilityHelper();
53
54 /** Ctor */
55 public LauncherClings(Launcher launcher) {
56 mLauncher = launcher;
57 mInflater = mLauncher.getLayoutInflater();
58 }
59
60 /** Initializes a cling */
61 private Cling initCling(int clingId, int scrimId, boolean animate,
62 boolean dimNavBarVisibilty) {
63 Cling cling = (Cling) mLauncher.findViewById(clingId);
64 View scrim = null;
65 if (scrimId > 0) {
Winson Chung205cd772014-01-15 14:31:59 -080066 scrim = mLauncher.findViewById(scrimId);
Winson Chunga6945242014-01-08 14:04:34 -080067 }
68 if (cling != null) {
69 cling.init(mLauncher, scrim);
70 cling.show(animate, SHOW_CLING_DURATION);
71
72 if (dimNavBarVisibilty) {
73 cling.setSystemUiVisibility(cling.getSystemUiVisibility() |
74 View.SYSTEM_UI_FLAG_LOW_PROFILE);
75 }
76 }
77 return cling;
78 }
79
80 /** Returns whether the clings are enabled or should be shown */
Winson Chung205cd772014-01-15 14:31:59 -080081 private boolean areClingsEnabled() {
Winson Chunga6945242014-01-08 14:04:34 -080082 if (DISABLE_CLINGS) {
83 return false;
84 }
85
Winson Chunga6945242014-01-08 14:04:34 -080086 // disable clings when running in a test harness
87 if(ActivityManager.isRunningInTestHarness()) return false;
88
89 // Disable clings for accessibility when explore by touch is enabled
90 final AccessibilityManager a11yManager = (AccessibilityManager) mLauncher.getSystemService(
91 Launcher.ACCESSIBILITY_SERVICE);
92 if (a11yManager.isTouchExplorationEnabled()) {
93 return false;
94 }
95
96 // Restricted secondary users (child mode) will potentially have very few apps
97 // seeded when they start up for the first time. Clings won't work well with that
98 boolean supportsLimitedUsers =
99 android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
100 Account[] accounts = AccountManager.get(mLauncher).getAccounts();
101 if (supportsLimitedUsers && accounts.length == 0) {
102 UserManager um = (UserManager) mLauncher.getSystemService(Context.USER_SERVICE);
103 Bundle restrictions = um.getUserRestrictions();
104 if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
105 return false;
106 }
107 }
108 return true;
109 }
110
111 /** Returns whether the folder cling is visible. */
112 public boolean isFolderClingVisible() {
113 Cling cling = (Cling) mLauncher.findViewById(R.id.folder_cling);
114 if (cling != null) {
115 return cling.getVisibility() == View.VISIBLE;
116 }
117 return false;
118 }
119
120 private boolean skipCustomClingIfNoAccounts() {
121 Cling cling = (Cling) mLauncher.findViewById(R.id.workspace_cling);
122 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
123 if (customCling) {
124 AccountManager am = AccountManager.get(mLauncher);
125 if (am == null) return false;
126 Account[] accounts = am.getAccountsByType("com.google");
127 return accounts.length == 0;
128 }
129 return false;
130 }
131
132 /** Updates the first run cling custom content hint */
133 private void setCustomContentHintVisibility(Cling cling, String ccHintStr, boolean visible,
134 boolean animate) {
135 final TextView ccHint = (TextView) cling.findViewById(R.id.custom_content_hint);
136 if (ccHint != null) {
137 if (visible && !ccHintStr.isEmpty()) {
138 ccHint.setText(ccHintStr);
139 ccHint.setVisibility(View.VISIBLE);
140 if (animate) {
141 ccHint.setAlpha(0f);
142 ccHint.animate().alpha(1f)
143 .setDuration(SHOW_CLING_DURATION)
144 .start();
145 } else {
146 ccHint.setAlpha(1f);
147 }
148 } else {
149 if (animate) {
150 ccHint.animate().alpha(0f)
151 .setDuration(SHOW_CLING_DURATION)
152 .setListener(new AnimatorListenerAdapter() {
153 @Override
154 public void onAnimationEnd(Animator animation) {
155 ccHint.setVisibility(View.GONE);
156 }
157 })
158 .start();
159 } else {
160 ccHint.setAlpha(0f);
161 ccHint.setVisibility(View.GONE);
162 }
163 }
164 }
165 }
166
167 /** Updates the first run cling custom content hint */
168 public void updateCustomContentHintVisibility() {
169 Cling cling = (Cling) mLauncher.findViewById(R.id.first_run_cling);
170 String ccHintStr = mLauncher.getFirstRunCustomContentHint();
171
172 if (mLauncher.getWorkspace().hasCustomContent()) {
173 // Show the custom content hint if ccHintStr is not empty
174 if (cling != null) {
175 setCustomContentHintVisibility(cling, ccHintStr, true, true);
176 }
177 } else {
178 // Hide the custom content hint
179 if (cling != null) {
180 setCustomContentHintVisibility(cling, ccHintStr, false, true);
181 }
182 }
183 }
184
185 /** Updates the first run cling search bar hint. */
186 public void updateSearchBarHint(String hint) {
187 Cling cling = (Cling) mLauncher.findViewById(R.id.first_run_cling);
188 if (cling != null && cling.getVisibility() == View.VISIBLE && !hint.isEmpty()) {
189 TextView sbHint = (TextView) cling.findViewById(R.id.search_bar_hint);
190 sbHint.setText(hint);
191 sbHint.setVisibility(View.VISIBLE);
192 }
193 }
194
Winson Chunge43a1e72014-01-15 10:33:02 -0800195 public boolean shouldShowFirstRunOrMigrationClings() {
Winson Chunga6945242014-01-08 14:04:34 -0800196 SharedPreferences sharedPrefs = mLauncher.getSharedPrefs();
Winson Chung205cd772014-01-15 14:31:59 -0800197 return areClingsEnabled() &&
Winson Chunge43a1e72014-01-15 10:33:02 -0800198 !sharedPrefs.getBoolean(FIRST_RUN_CLING_DISMISSED_KEY, false) &&
199 !sharedPrefs.getBoolean(MIGRATION_CLING_DISMISSED_KEY, false);
200 }
Winson Chunga6945242014-01-08 14:04:34 -0800201
Winson Chunge43a1e72014-01-15 10:33:02 -0800202 public void removeFirstRunAndMigrationClings() {
203 removeCling(R.id.first_run_cling);
204 removeCling(R.id.migration_cling);
205 }
Winson Chunga6945242014-01-08 14:04:34 -0800206
Winson Chunge43a1e72014-01-15 10:33:02 -0800207 /**
208 * Shows the first run cling.
209 *
210 * This flow is mutually exclusive with showMigrationCling, and only runs if this Launcher
211 * package was preinstalled or there is no db to migrate from.
212 */
213 public void showFirstRunCling() {
214 if (!skipCustomClingIfNoAccounts()) {
215 SharedPreferences sharedPrefs = mLauncher.getSharedPrefs();
Winson Chunga6945242014-01-08 14:04:34 -0800216 // If we're not using the default workspace layout, replace workspace cling
217 // with a custom workspace cling (usually specified in an overlay)
218 // For now, only do this on tablets
219 if (!DISABLE_CUSTOM_CLINGS) {
220 if (sharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
221 mLauncher.getResources().getBoolean(R.bool.config_useCustomClings)) {
222 // Use a custom cling
223 View cling = mLauncher.findViewById(R.id.workspace_cling);
224 ViewGroup clingParent = (ViewGroup) cling.getParent();
225 int clingIndex = clingParent.indexOfChild(cling);
226 clingParent.removeViewAt(clingIndex);
227 View customCling = mInflater.inflate(R.layout.custom_workspace_cling,
228 clingParent, false);
229 clingParent.addView(customCling, clingIndex);
230 customCling.setId(R.id.workspace_cling);
231 }
232 }
233 Cling cling = (Cling) mLauncher.findViewById(R.id.first_run_cling);
234 if (cling != null) {
235 String sbHintStr = mLauncher.getFirstRunClingSearchBarHint();
236 String ccHintStr = mLauncher.getFirstRunCustomContentHint();
237 if (!sbHintStr.isEmpty()) {
238 TextView sbHint = (TextView) cling.findViewById(R.id.search_bar_hint);
239 sbHint.setText(sbHintStr);
240 sbHint.setVisibility(View.VISIBLE);
241 }
242 setCustomContentHintVisibility(cling, ccHintStr, true, false);
243 }
244 initCling(R.id.first_run_cling, 0, false, true);
245 } else {
Winson Chunge43a1e72014-01-15 10:33:02 -0800246 removeFirstRunAndMigrationClings();
Winson Chunga6945242014-01-08 14:04:34 -0800247 }
248 }
249
Winson Chunge43a1e72014-01-15 10:33:02 -0800250 /**
251 * Shows the migration cling.
252 *
253 * This flow is mutually exclusive with showFirstRunCling, and only runs if this Launcher
254 * package was not preinstalled and there exists a db to migrate from.
255 */
Winson Chunga6945242014-01-08 14:04:34 -0800256 public void showMigrationCling() {
Winson Chunge43a1e72014-01-15 10:33:02 -0800257 mLauncher.hideWorkspaceSearchAndHotseat();
Winson Chunga6945242014-01-08 14:04:34 -0800258
Winson Chunge43a1e72014-01-15 10:33:02 -0800259 Cling c = initCling(R.id.migration_cling, 0, false, true);
260 c.bringScrimToFront();
261 c.bringToFront();
Winson Chunga6945242014-01-08 14:04:34 -0800262 }
263
264 public void showMigrationWorkspaceCling() {
265 // Enable the clings only if they have not been dismissed before
Winson Chung205cd772014-01-15 14:31:59 -0800266 if (areClingsEnabled() && !mLauncher.getSharedPrefs().getBoolean(
Winson Chunga6945242014-01-08 14:04:34 -0800267 MIGRATION_WORKSPACE_CLING_DISMISSED_KEY, false)) {
268 Cling c = initCling(R.id.migration_workspace_cling, 0, false, true);
269 c.updateMigrationWorkspaceBubblePosition();
270 c.bringScrimToFront();
271 c.bringToFront();
272 } else {
273 removeCling(R.id.migration_workspace_cling);
274 }
275 }
276
277 public void showWorkspaceCling() {
278 // Enable the clings only if they have not been dismissed before
Winson Chung205cd772014-01-15 14:31:59 -0800279 if (areClingsEnabled() && !mLauncher.getSharedPrefs().getBoolean(
Winson Chunga6945242014-01-08 14:04:34 -0800280 WORKSPACE_CLING_DISMISSED_KEY, false)) {
281 Cling c = initCling(R.id.workspace_cling, 0, false, true);
Winson Chung205cd772014-01-15 14:31:59 -0800282 c.updateWorkspaceBubblePosition();
Winson Chunga6945242014-01-08 14:04:34 -0800283
284 // Set the focused hotseat app if there is one
285 c.setFocusedHotseatApp(mLauncher.getFirstRunFocusedHotseatAppDrawableId(),
286 mLauncher.getFirstRunFocusedHotseatAppRank(),
287 mLauncher.getFirstRunFocusedHotseatAppComponentName(),
288 mLauncher.getFirstRunFocusedHotseatAppBubbleTitle(),
289 mLauncher.getFirstRunFocusedHotseatAppBubbleDescription());
290 } else {
291 removeCling(R.id.workspace_cling);
292 }
293 }
294 public Cling showFoldersCling() {
295 SharedPreferences sharedPrefs = mLauncher.getSharedPrefs();
296 // Enable the clings only if they have not been dismissed before
Winson Chung205cd772014-01-15 14:31:59 -0800297 if (areClingsEnabled() &&
Winson Chunga6945242014-01-08 14:04:34 -0800298 !sharedPrefs.getBoolean(FOLDER_CLING_DISMISSED_KEY, false) &&
299 !sharedPrefs.getBoolean(Launcher.USER_HAS_MIGRATED, false)) {
300 Cling cling = initCling(R.id.folder_cling, R.id.cling_scrim,
301 true, true);
Winson Chung205cd772014-01-15 14:31:59 -0800302 Folder openFolder = mLauncher.getWorkspace().getOpenFolder();
303 if (openFolder != null) {
304 Rect openFolderRect = new Rect();
305 openFolder.getHitRect(openFolderRect);
306 cling.setOpenFolderRect(openFolderRect);
307 openFolder.bringToFront();
308 }
Winson Chunga6945242014-01-08 14:04:34 -0800309 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}