Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher3; |
| 18 | |
| 19 | import android.accounts.Account; |
| 20 | import android.accounts.AccountManager; |
Sunny Goyal | 424418b | 2014-08-22 16:09:37 -0700 | [diff] [blame] | 21 | import android.animation.ObjectAnimator; |
| 22 | import android.animation.PropertyValuesHolder; |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame^] | 23 | import android.annotation.TargetApi; |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 24 | import android.app.ActivityManager; |
| 25 | import android.content.Context; |
| 26 | import android.content.SharedPreferences; |
Sunny Goyal | 7661614 | 2014-08-26 10:54:08 -0700 | [diff] [blame] | 27 | import android.graphics.drawable.Drawable; |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame^] | 28 | import android.os.Build; |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 29 | import android.os.Bundle; |
| 30 | import android.os.UserManager; |
Amith Yamasani | 5ba21b6 | 2014-06-25 16:14:02 +0530 | [diff] [blame] | 31 | import android.provider.Settings; |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 32 | import android.view.LayoutInflater; |
| 33 | import android.view.View; |
Sunny Goyal | 424418b | 2014-08-22 16:09:37 -0700 | [diff] [blame] | 34 | import android.view.View.OnClickListener; |
| 35 | import android.view.View.OnLongClickListener; |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 36 | import android.view.ViewGroup; |
Sunny Goyal | 424418b | 2014-08-22 16:09:37 -0700 | [diff] [blame] | 37 | import android.view.ViewTreeObserver.OnGlobalLayoutListener; |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 38 | import android.view.accessibility.AccessibilityManager; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 39 | import com.android.launcher3.util.Thunk; |
| 40 | |
Sunny Goyal | 424418b | 2014-08-22 16:09:37 -0700 | [diff] [blame] | 41 | class LauncherClings implements OnClickListener { |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 42 | private static final String MIGRATION_CLING_DISMISSED_KEY = "cling_gel.migration.dismissed"; |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 43 | private static final String WORKSPACE_CLING_DISMISSED_KEY = "cling_gel.workspace.dismissed"; |
Sunny Goyal | 424418b | 2014-08-22 16:09:37 -0700 | [diff] [blame] | 44 | |
Sunny Goyal | 7661614 | 2014-08-26 10:54:08 -0700 | [diff] [blame] | 45 | private static final String TAG_CROP_TOP_AND_SIDES = "crop_bg_top_and_sides"; |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 46 | |
| 47 | private static final boolean DISABLE_CLINGS = false; |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 48 | |
| 49 | private static final int SHOW_CLING_DURATION = 250; |
| 50 | private static final int DISMISS_CLING_DURATION = 200; |
| 51 | |
Amith Yamasani | 5ba21b6 | 2014-06-25 16:14:02 +0530 | [diff] [blame] | 52 | // New Secure Setting in L |
| 53 | private static final String SKIP_FIRST_USE_HINTS = "skip_first_use_hints"; |
| 54 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 55 | @Thunk Launcher mLauncher; |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 56 | private LayoutInflater mInflater; |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 57 | |
| 58 | /** Ctor */ |
| 59 | public LauncherClings(Launcher launcher) { |
| 60 | mLauncher = launcher; |
Sunny Goyal | e03b812 | 2014-10-08 09:55:24 -0700 | [diff] [blame] | 61 | mInflater = LayoutInflater.from(mLauncher); |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Sunny Goyal | 424418b | 2014-08-22 16:09:37 -0700 | [diff] [blame] | 64 | @Override |
| 65 | public void onClick(View v) { |
| 66 | int id = v.getId(); |
| 67 | if (id == R.id.cling_dismiss_migration_use_default) { |
| 68 | // Disable the migration cling |
| 69 | dismissMigrationCling(); |
| 70 | } else if (id == R.id.cling_dismiss_migration_copy_apps) { |
| 71 | // Copy the shortcuts from the old database |
| 72 | LauncherModel model = mLauncher.getModel(); |
| 73 | model.resetLoadedState(false, true); |
| 74 | model.startLoader(false, PagedView.INVALID_RESTORE_PAGE, |
| 75 | LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE |
| 76 | | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS); |
| 77 | // Set the flag to skip the folder cling |
| 78 | String spKey = LauncherAppState.getSharedPreferencesKey(); |
| 79 | SharedPreferences sp = mLauncher.getSharedPreferences(spKey, Context.MODE_PRIVATE); |
| 80 | SharedPreferences.Editor editor = sp.edit(); |
| 81 | editor.putBoolean(Launcher.USER_HAS_MIGRATED, true); |
| 82 | editor.apply(); |
| 83 | // Disable the migration cling |
| 84 | dismissMigrationCling(); |
| 85 | } else if (id == R.id.cling_dismiss_longpress_info) { |
| 86 | dismissLongPressCling(); |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 87 | } |
Sunny Goyal | 424418b | 2014-08-22 16:09:37 -0700 | [diff] [blame] | 88 | } |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 89 | |
Sunny Goyal | 424418b | 2014-08-22 16:09:37 -0700 | [diff] [blame] | 90 | /** |
| 91 | * Shows the migration cling. |
| 92 | * |
| 93 | * This flow is mutually exclusive with showFirstRunCling, and only runs if this Launcher |
| 94 | * package was not preinstalled and there exists a db to migrate from. |
| 95 | */ |
| 96 | public void showMigrationCling() { |
| 97 | mLauncher.hideWorkspaceSearchAndHotseat(); |
| 98 | |
| 99 | ViewGroup root = (ViewGroup) mLauncher.findViewById(R.id.launcher); |
| 100 | View inflated = mInflater.inflate(R.layout.migration_cling, root); |
| 101 | inflated.findViewById(R.id.cling_dismiss_migration_copy_apps).setOnClickListener(this); |
| 102 | inflated.findViewById(R.id.cling_dismiss_migration_use_default).setOnClickListener(this); |
| 103 | } |
| 104 | |
| 105 | private void dismissMigrationCling() { |
| 106 | mLauncher.showWorkspaceSearchAndHotseat(); |
| 107 | Runnable dismissCb = new Runnable() { |
| 108 | public void run() { |
| 109 | Runnable cb = new Runnable() { |
| 110 | public void run() { |
| 111 | // Show the longpress cling next |
| 112 | showLongPressCling(false); |
| 113 | } |
| 114 | }; |
| 115 | dismissCling(mLauncher.findViewById(R.id.migration_cling), cb, |
| 116 | MIGRATION_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION); |
| 117 | } |
| 118 | }; |
| 119 | mLauncher.getWorkspace().post(dismissCb); |
| 120 | } |
| 121 | |
| 122 | public void showLongPressCling(boolean showWelcome) { |
| 123 | ViewGroup root = (ViewGroup) mLauncher.findViewById(R.id.launcher); |
| 124 | View cling = mInflater.inflate(R.layout.longpress_cling, root, false); |
| 125 | |
Sunny Goyal | 7661614 | 2014-08-26 10:54:08 -0700 | [diff] [blame] | 126 | cling.setOnLongClickListener(new OnLongClickListener() { |
Sunny Goyal | 424418b | 2014-08-22 16:09:37 -0700 | [diff] [blame] | 127 | |
| 128 | @Override |
| 129 | public boolean onLongClick(View v) { |
Winson Chung | dc61c4d | 2015-04-20 18:26:57 -0700 | [diff] [blame] | 130 | mLauncher.showOverviewMode(true); |
Sunny Goyal | 424418b | 2014-08-22 16:09:37 -0700 | [diff] [blame] | 131 | dismissLongPressCling(); |
| 132 | return true; |
| 133 | } |
| 134 | }); |
| 135 | |
| 136 | final ViewGroup content = (ViewGroup) cling.findViewById(R.id.cling_content); |
| 137 | mInflater.inflate(showWelcome ? R.layout.longpress_cling_welcome_content |
| 138 | : R.layout.longpress_cling_content, content); |
| 139 | content.findViewById(R.id.cling_dismiss_longpress_info).setOnClickListener(this); |
| 140 | |
Sunny Goyal | 7661614 | 2014-08-26 10:54:08 -0700 | [diff] [blame] | 141 | if (TAG_CROP_TOP_AND_SIDES.equals(content.getTag())) { |
| 142 | Drawable bg = new BorderCropDrawable(mLauncher.getResources().getDrawable(R.drawable.cling_bg), |
| 143 | true, true, true, false); |
| 144 | content.setBackground(bg); |
| 145 | } |
| 146 | |
Sunny Goyal | 424418b | 2014-08-22 16:09:37 -0700 | [diff] [blame] | 147 | root.addView(cling); |
| 148 | |
| 149 | if (showWelcome) { |
| 150 | // This is the first cling being shown. No need to animate. |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | // Animate |
| 155 | content.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { |
| 156 | |
| 157 | @Override |
| 158 | public void onGlobalLayout() { |
| 159 | content.getViewTreeObserver().removeOnGlobalLayoutListener(this); |
| 160 | |
Sunny Goyal | 7661614 | 2014-08-26 10:54:08 -0700 | [diff] [blame] | 161 | ObjectAnimator anim; |
| 162 | if (TAG_CROP_TOP_AND_SIDES.equals(content.getTag())) { |
Sunny Goyal | 424418b | 2014-08-22 16:09:37 -0700 | [diff] [blame] | 163 | content.setTranslationY(-content.getMeasuredHeight()); |
Sunny Goyal | 7661614 | 2014-08-26 10:54:08 -0700 | [diff] [blame] | 164 | anim = LauncherAnimUtils.ofFloat(content, "translationY", 0); |
Sunny Goyal | 424418b | 2014-08-22 16:09:37 -0700 | [diff] [blame] | 165 | } else { |
| 166 | content.setScaleX(0); |
| 167 | content.setScaleY(0); |
| 168 | PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1); |
| 169 | PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1); |
Sunny Goyal | 7661614 | 2014-08-26 10:54:08 -0700 | [diff] [blame] | 170 | anim = LauncherAnimUtils.ofPropertyValuesHolder(content, scaleX, scaleY); |
Sunny Goyal | 424418b | 2014-08-22 16:09:37 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Sunny Goyal | 424418b | 2014-08-22 16:09:37 -0700 | [diff] [blame] | 173 | anim.setDuration(SHOW_CLING_DURATION); |
| 174 | anim.setInterpolator(new LogDecelerateInterpolator(100, 0)); |
Sunny Goyal | 424418b | 2014-08-22 16:09:37 -0700 | [diff] [blame] | 175 | anim.start(); |
| 176 | } |
| 177 | }); |
| 178 | } |
| 179 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 180 | @Thunk void dismissLongPressCling() { |
Sunny Goyal | 424418b | 2014-08-22 16:09:37 -0700 | [diff] [blame] | 181 | Runnable dismissCb = new Runnable() { |
| 182 | public void run() { |
| 183 | dismissCling(mLauncher.findViewById(R.id.longpress_cling), null, |
| 184 | WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION); |
| 185 | } |
| 186 | }; |
| 187 | mLauncher.getWorkspace().post(dismissCb); |
| 188 | } |
| 189 | |
| 190 | /** Hides the specified Cling */ |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 191 | @Thunk void dismissCling(final View cling, final Runnable postAnimationCb, |
Sunny Goyal | 424418b | 2014-08-22 16:09:37 -0700 | [diff] [blame] | 192 | final String flag, int duration) { |
| 193 | // To catch cases where siblings of top-level views are made invisible, just check whether |
| 194 | // the cling is directly set to GONE before dismissing it. |
| 195 | if (cling != null && cling.getVisibility() != View.GONE) { |
| 196 | final Runnable cleanUpClingCb = new Runnable() { |
| 197 | public void run() { |
| 198 | cling.setVisibility(View.GONE); |
| 199 | mLauncher.getSharedPrefs().edit() |
| 200 | .putBoolean(flag, true) |
| 201 | .apply(); |
| 202 | if (postAnimationCb != null) { |
| 203 | postAnimationCb.run(); |
| 204 | } |
| 205 | } |
| 206 | }; |
| 207 | if (duration <= 0) { |
| 208 | cleanUpClingCb.run(); |
| 209 | } else { |
| 210 | cling.animate().alpha(0).setDuration(duration).withEndAction(cleanUpClingCb); |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 211 | } |
| 212 | } |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** Returns whether the clings are enabled or should be shown */ |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame^] | 216 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) |
Winson Chung | 205cd77 | 2014-01-15 14:31:59 -0800 | [diff] [blame] | 217 | private boolean areClingsEnabled() { |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 218 | if (DISABLE_CLINGS) { |
| 219 | return false; |
| 220 | } |
| 221 | |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 222 | // disable clings when running in a test harness |
| 223 | if(ActivityManager.isRunningInTestHarness()) return false; |
| 224 | |
| 225 | // Disable clings for accessibility when explore by touch is enabled |
| 226 | final AccessibilityManager a11yManager = (AccessibilityManager) mLauncher.getSystemService( |
| 227 | Launcher.ACCESSIBILITY_SERVICE); |
| 228 | if (a11yManager.isTouchExplorationEnabled()) { |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | // Restricted secondary users (child mode) will potentially have very few apps |
| 233 | // seeded when they start up for the first time. Clings won't work well with that |
| 234 | boolean supportsLimitedUsers = |
| 235 | android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2; |
| 236 | Account[] accounts = AccountManager.get(mLauncher).getAccounts(); |
| 237 | if (supportsLimitedUsers && accounts.length == 0) { |
| 238 | UserManager um = (UserManager) mLauncher.getSystemService(Context.USER_SERVICE); |
| 239 | Bundle restrictions = um.getUserRestrictions(); |
| 240 | if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) { |
| 241 | return false; |
| 242 | } |
| 243 | } |
Amith Yamasani | 5ba21b6 | 2014-06-25 16:14:02 +0530 | [diff] [blame] | 244 | if (Settings.Secure.getInt(mLauncher.getContentResolver(), SKIP_FIRST_USE_HINTS, 0) |
| 245 | == 1) { |
| 246 | return false; |
| 247 | } |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 248 | return true; |
| 249 | } |
| 250 | |
Winson Chung | e43a1e7 | 2014-01-15 10:33:02 -0800 | [diff] [blame] | 251 | public boolean shouldShowFirstRunOrMigrationClings() { |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 252 | SharedPreferences sharedPrefs = mLauncher.getSharedPrefs(); |
Winson Chung | 205cd77 | 2014-01-15 14:31:59 -0800 | [diff] [blame] | 253 | return areClingsEnabled() && |
Sunny Goyal | 424418b | 2014-08-22 16:09:37 -0700 | [diff] [blame] | 254 | !sharedPrefs.getBoolean(WORKSPACE_CLING_DISMISSED_KEY, false) && |
Adam Cohen | 71e03b9 | 2014-02-21 14:09:53 -0800 | [diff] [blame] | 255 | !sharedPrefs.getBoolean(MIGRATION_CLING_DISMISSED_KEY, false); |
Winson Chung | e43a1e7 | 2014-01-15 10:33:02 -0800 | [diff] [blame] | 256 | } |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame] | 257 | |
Adam Cohen | 71e03b9 | 2014-02-21 14:09:53 -0800 | [diff] [blame] | 258 | public static void synchonouslyMarkFirstRunClingDismissed(Context ctx) { |
| 259 | SharedPreferences prefs = ctx.getSharedPreferences( |
Winson Chung | c6c0367 | 2014-03-06 10:12:02 -0800 | [diff] [blame] | 260 | LauncherAppState.getSharedPreferencesKey(), Context.MODE_PRIVATE); |
Adam Cohen | 71e03b9 | 2014-02-21 14:09:53 -0800 | [diff] [blame] | 261 | SharedPreferences.Editor editor = prefs.edit(); |
Sunny Goyal | 424418b | 2014-08-22 16:09:37 -0700 | [diff] [blame] | 262 | editor.putBoolean(WORKSPACE_CLING_DISMISSED_KEY, true); |
Adam Cohen | 71e03b9 | 2014-02-21 14:09:53 -0800 | [diff] [blame] | 263 | editor.commit(); |
| 264 | } |
Adam Cohen | 71e03b9 | 2014-02-21 14:09:53 -0800 | [diff] [blame] | 265 | } |