blob: ef8e8abcfe5dd3b4b7a1edcf4d7d22db63dd0e41 [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;
Sunny Goyal424418b2014-08-22 16:09:37 -070021import android.animation.ObjectAnimator;
22import android.animation.PropertyValuesHolder;
Winson Chunga6945242014-01-08 14:04:34 -080023import android.app.ActivityManager;
24import android.content.Context;
25import android.content.SharedPreferences;
Sunny Goyal76616142014-08-26 10:54:08 -070026import android.graphics.drawable.Drawable;
Winson Chunga6945242014-01-08 14:04:34 -080027import android.os.Bundle;
28import android.os.UserManager;
Amith Yamasani5ba21b62014-06-25 16:14:02 +053029import android.provider.Settings;
Winson Chunga6945242014-01-08 14:04:34 -080030import android.view.LayoutInflater;
31import android.view.View;
Sunny Goyal424418b2014-08-22 16:09:37 -070032import android.view.View.OnClickListener;
33import android.view.View.OnLongClickListener;
Winson Chunga6945242014-01-08 14:04:34 -080034import android.view.ViewGroup;
Sunny Goyal424418b2014-08-22 16:09:37 -070035import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Winson Chunga6945242014-01-08 14:04:34 -080036import android.view.accessibility.AccessibilityManager;
Winson Chunga6945242014-01-08 14:04:34 -080037
Sunny Goyal424418b2014-08-22 16:09:37 -070038class LauncherClings implements OnClickListener {
Winson Chunga6945242014-01-08 14:04:34 -080039 private static final String MIGRATION_CLING_DISMISSED_KEY = "cling_gel.migration.dismissed";
Winson Chunga6945242014-01-08 14:04:34 -080040 private static final String WORKSPACE_CLING_DISMISSED_KEY = "cling_gel.workspace.dismissed";
Sunny Goyal424418b2014-08-22 16:09:37 -070041
Sunny Goyal76616142014-08-26 10:54:08 -070042 private static final String TAG_CROP_TOP_AND_SIDES = "crop_bg_top_and_sides";
Winson Chunga6945242014-01-08 14:04:34 -080043
44 private static final boolean DISABLE_CLINGS = false;
Winson Chunga6945242014-01-08 14:04:34 -080045
46 private static final int SHOW_CLING_DURATION = 250;
47 private static final int DISMISS_CLING_DURATION = 200;
48
Amith Yamasani5ba21b62014-06-25 16:14:02 +053049 // New Secure Setting in L
50 private static final String SKIP_FIRST_USE_HINTS = "skip_first_use_hints";
51
Winson Chunga6945242014-01-08 14:04:34 -080052 private Launcher mLauncher;
53 private LayoutInflater mInflater;
Winson Chunga6945242014-01-08 14:04:34 -080054
55 /** Ctor */
56 public LauncherClings(Launcher launcher) {
57 mLauncher = launcher;
Sunny Goyale03b8122014-10-08 09:55:24 -070058 mInflater = LayoutInflater.from(mLauncher);
Winson Chunga6945242014-01-08 14:04:34 -080059 }
60
Sunny Goyal424418b2014-08-22 16:09:37 -070061 @Override
62 public void onClick(View v) {
63 int id = v.getId();
64 if (id == R.id.cling_dismiss_migration_use_default) {
65 // Disable the migration cling
66 dismissMigrationCling();
67 } else if (id == R.id.cling_dismiss_migration_copy_apps) {
68 // Copy the shortcuts from the old database
69 LauncherModel model = mLauncher.getModel();
70 model.resetLoadedState(false, true);
71 model.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
72 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
73 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
74 // Set the flag to skip the folder cling
75 String spKey = LauncherAppState.getSharedPreferencesKey();
76 SharedPreferences sp = mLauncher.getSharedPreferences(spKey, Context.MODE_PRIVATE);
77 SharedPreferences.Editor editor = sp.edit();
78 editor.putBoolean(Launcher.USER_HAS_MIGRATED, true);
79 editor.apply();
80 // Disable the migration cling
81 dismissMigrationCling();
82 } else if (id == R.id.cling_dismiss_longpress_info) {
83 dismissLongPressCling();
Winson Chunga6945242014-01-08 14:04:34 -080084 }
Sunny Goyal424418b2014-08-22 16:09:37 -070085 }
Winson Chunga6945242014-01-08 14:04:34 -080086
Sunny Goyal424418b2014-08-22 16:09:37 -070087 /**
88 * Shows the migration cling.
89 *
90 * This flow is mutually exclusive with showFirstRunCling, and only runs if this Launcher
91 * package was not preinstalled and there exists a db to migrate from.
92 */
93 public void showMigrationCling() {
94 mLauncher.hideWorkspaceSearchAndHotseat();
95
96 ViewGroup root = (ViewGroup) mLauncher.findViewById(R.id.launcher);
97 View inflated = mInflater.inflate(R.layout.migration_cling, root);
98 inflated.findViewById(R.id.cling_dismiss_migration_copy_apps).setOnClickListener(this);
99 inflated.findViewById(R.id.cling_dismiss_migration_use_default).setOnClickListener(this);
100 }
101
102 private void dismissMigrationCling() {
103 mLauncher.showWorkspaceSearchAndHotseat();
104 Runnable dismissCb = new Runnable() {
105 public void run() {
106 Runnable cb = new Runnable() {
107 public void run() {
108 // Show the longpress cling next
109 showLongPressCling(false);
110 }
111 };
112 dismissCling(mLauncher.findViewById(R.id.migration_cling), cb,
113 MIGRATION_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
114 }
115 };
116 mLauncher.getWorkspace().post(dismissCb);
117 }
118
119 public void showLongPressCling(boolean showWelcome) {
120 ViewGroup root = (ViewGroup) mLauncher.findViewById(R.id.launcher);
121 View cling = mInflater.inflate(R.layout.longpress_cling, root, false);
122
Sunny Goyal76616142014-08-26 10:54:08 -0700123 cling.setOnLongClickListener(new OnLongClickListener() {
Sunny Goyal424418b2014-08-22 16:09:37 -0700124
125 @Override
126 public boolean onLongClick(View v) {
127 mLauncher.getWorkspace().enterOverviewMode();
128 dismissLongPressCling();
129 return true;
130 }
131 });
132
133 final ViewGroup content = (ViewGroup) cling.findViewById(R.id.cling_content);
134 mInflater.inflate(showWelcome ? R.layout.longpress_cling_welcome_content
135 : R.layout.longpress_cling_content, content);
136 content.findViewById(R.id.cling_dismiss_longpress_info).setOnClickListener(this);
137
Sunny Goyal76616142014-08-26 10:54:08 -0700138 if (TAG_CROP_TOP_AND_SIDES.equals(content.getTag())) {
139 Drawable bg = new BorderCropDrawable(mLauncher.getResources().getDrawable(R.drawable.cling_bg),
140 true, true, true, false);
141 content.setBackground(bg);
142 }
143
Sunny Goyal424418b2014-08-22 16:09:37 -0700144 root.addView(cling);
145
146 if (showWelcome) {
147 // This is the first cling being shown. No need to animate.
148 return;
149 }
150
151 // Animate
152 content.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
153
154 @Override
155 public void onGlobalLayout() {
156 content.getViewTreeObserver().removeOnGlobalLayoutListener(this);
157
Sunny Goyal76616142014-08-26 10:54:08 -0700158 ObjectAnimator anim;
159 if (TAG_CROP_TOP_AND_SIDES.equals(content.getTag())) {
Sunny Goyal424418b2014-08-22 16:09:37 -0700160 content.setTranslationY(-content.getMeasuredHeight());
Sunny Goyal76616142014-08-26 10:54:08 -0700161 anim = LauncherAnimUtils.ofFloat(content, "translationY", 0);
Sunny Goyal424418b2014-08-22 16:09:37 -0700162 } else {
163 content.setScaleX(0);
164 content.setScaleY(0);
165 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1);
166 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1);
Sunny Goyal76616142014-08-26 10:54:08 -0700167 anim = LauncherAnimUtils.ofPropertyValuesHolder(content, scaleX, scaleY);
Sunny Goyal424418b2014-08-22 16:09:37 -0700168 }
169
Sunny Goyal424418b2014-08-22 16:09:37 -0700170 anim.setDuration(SHOW_CLING_DURATION);
171 anim.setInterpolator(new LogDecelerateInterpolator(100, 0));
Sunny Goyal424418b2014-08-22 16:09:37 -0700172 anim.start();
173 }
174 });
175 }
176
177 private void dismissLongPressCling() {
178 Runnable dismissCb = new Runnable() {
179 public void run() {
180 dismissCling(mLauncher.findViewById(R.id.longpress_cling), null,
181 WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
182 }
183 };
184 mLauncher.getWorkspace().post(dismissCb);
185 }
186
187 /** Hides the specified Cling */
188 private void dismissCling(final View cling, final Runnable postAnimationCb,
189 final String flag, int duration) {
190 // To catch cases where siblings of top-level views are made invisible, just check whether
191 // the cling is directly set to GONE before dismissing it.
192 if (cling != null && cling.getVisibility() != View.GONE) {
193 final Runnable cleanUpClingCb = new Runnable() {
194 public void run() {
195 cling.setVisibility(View.GONE);
196 mLauncher.getSharedPrefs().edit()
197 .putBoolean(flag, true)
198 .apply();
199 if (postAnimationCb != null) {
200 postAnimationCb.run();
201 }
202 }
203 };
204 if (duration <= 0) {
205 cleanUpClingCb.run();
206 } else {
207 cling.animate().alpha(0).setDuration(duration).withEndAction(cleanUpClingCb);
Winson Chunga6945242014-01-08 14:04:34 -0800208 }
209 }
Winson Chunga6945242014-01-08 14:04:34 -0800210 }
211
212 /** Returns whether the clings are enabled or should be shown */
Winson Chung205cd772014-01-15 14:31:59 -0800213 private boolean areClingsEnabled() {
Winson Chunga6945242014-01-08 14:04:34 -0800214 if (DISABLE_CLINGS) {
215 return false;
216 }
217
Winson Chunga6945242014-01-08 14:04:34 -0800218 // disable clings when running in a test harness
219 if(ActivityManager.isRunningInTestHarness()) return false;
220
221 // Disable clings for accessibility when explore by touch is enabled
222 final AccessibilityManager a11yManager = (AccessibilityManager) mLauncher.getSystemService(
223 Launcher.ACCESSIBILITY_SERVICE);
224 if (a11yManager.isTouchExplorationEnabled()) {
225 return false;
226 }
227
228 // Restricted secondary users (child mode) will potentially have very few apps
229 // seeded when they start up for the first time. Clings won't work well with that
230 boolean supportsLimitedUsers =
231 android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
232 Account[] accounts = AccountManager.get(mLauncher).getAccounts();
233 if (supportsLimitedUsers && accounts.length == 0) {
234 UserManager um = (UserManager) mLauncher.getSystemService(Context.USER_SERVICE);
235 Bundle restrictions = um.getUserRestrictions();
236 if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
237 return false;
238 }
239 }
Amith Yamasani5ba21b62014-06-25 16:14:02 +0530240 if (Settings.Secure.getInt(mLauncher.getContentResolver(), SKIP_FIRST_USE_HINTS, 0)
241 == 1) {
242 return false;
243 }
Winson Chunga6945242014-01-08 14:04:34 -0800244 return true;
245 }
246
Winson Chunge43a1e72014-01-15 10:33:02 -0800247 public boolean shouldShowFirstRunOrMigrationClings() {
Winson Chunga6945242014-01-08 14:04:34 -0800248 SharedPreferences sharedPrefs = mLauncher.getSharedPrefs();
Winson Chung205cd772014-01-15 14:31:59 -0800249 return areClingsEnabled() &&
Sunny Goyal424418b2014-08-22 16:09:37 -0700250 !sharedPrefs.getBoolean(WORKSPACE_CLING_DISMISSED_KEY, false) &&
Adam Cohen71e03b92014-02-21 14:09:53 -0800251 !sharedPrefs.getBoolean(MIGRATION_CLING_DISMISSED_KEY, false);
Winson Chunge43a1e72014-01-15 10:33:02 -0800252 }
Winson Chunga6945242014-01-08 14:04:34 -0800253
Adam Cohen71e03b92014-02-21 14:09:53 -0800254 public static void synchonouslyMarkFirstRunClingDismissed(Context ctx) {
255 SharedPreferences prefs = ctx.getSharedPreferences(
Winson Chungc6c03672014-03-06 10:12:02 -0800256 LauncherAppState.getSharedPreferencesKey(), Context.MODE_PRIVATE);
Adam Cohen71e03b92014-02-21 14:09:53 -0800257 SharedPreferences.Editor editor = prefs.edit();
Sunny Goyal424418b2014-08-22 16:09:37 -0700258 editor.putBoolean(WORKSPACE_CLING_DISMISSED_KEY, true);
Adam Cohen71e03b92014-02-21 14:09:53 -0800259 editor.commit();
260 }
Adam Cohen71e03b92014-02-21 14:09:53 -0800261}