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