Home | History | Annotate | Download | only in launcher3
      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.animation.ObjectAnimator;
     20 import android.animation.PropertyValuesHolder;
     21 import android.annotation.TargetApi;
     22 import android.app.ActivityManager;
     23 import android.content.Context;
     24 import android.content.SharedPreferences;
     25 import android.graphics.drawable.Drawable;
     26 import android.os.Build;
     27 import android.os.Bundle;
     28 import android.os.UserManager;
     29 import android.provider.Settings;
     30 import android.view.LayoutInflater;
     31 import android.view.View;
     32 import android.view.View.OnClickListener;
     33 import android.view.View.OnLongClickListener;
     34 import android.view.ViewGroup;
     35 import android.view.ViewTreeObserver.OnGlobalLayoutListener;
     36 import android.view.accessibility.AccessibilityManager;
     37 
     38 import com.android.launcher3.util.Thunk;
     39 
     40 class LauncherClings implements OnClickListener {
     41     private static final String MIGRATION_CLING_DISMISSED_KEY = "cling_gel.migration.dismissed";
     42     private static final String WORKSPACE_CLING_DISMISSED_KEY = "cling_gel.workspace.dismissed";
     43 
     44     private static final String TAG_CROP_TOP_AND_SIDES = "crop_bg_top_and_sides";
     45 
     46     private static final int SHOW_CLING_DURATION = 250;
     47     private static final int DISMISS_CLING_DURATION = 200;
     48 
     49     // New Secure Setting in L
     50     private static final String SKIP_FIRST_USE_HINTS = "skip_first_use_hints";
     51 
     52     @Thunk Launcher mLauncher;
     53     private LayoutInflater mInflater;
     54     @Thunk boolean mIsVisible;
     55 
     56     /** Ctor */
     57     public LauncherClings(Launcher launcher) {
     58         mLauncher = launcher;
     59         mInflater = LayoutInflater.from(mLauncher);
     60     }
     61 
     62     @Override
     63     public void onClick(View v) {
     64         int id = v.getId();
     65         if (id == R.id.cling_dismiss_migration_use_default) {
     66             // Disable the migration cling
     67             dismissMigrationCling();
     68         } else if (id == R.id.cling_dismiss_migration_copy_apps) {
     69             // Copy the shortcuts from the old database
     70             LauncherModel model = mLauncher.getModel();
     71             model.resetLoadedState(false, true);
     72             model.startLoader(PagedView.INVALID_RESTORE_PAGE,
     73                     LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
     74                             | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
     75             // Set the flag to skip the folder cling
     76             SharedPreferences.Editor editor = Utilities.getPrefs(mLauncher).edit();
     77             editor.putBoolean(Launcher.USER_HAS_MIGRATED, true);
     78             editor.apply();
     79             // Disable the migration cling
     80             dismissMigrationCling();
     81         } else if (id == R.id.cling_dismiss_longpress_info) {
     82             dismissLongPressCling();
     83         }
     84     }
     85 
     86     /**
     87      * Shows the migration cling.
     88      *
     89      * This flow is mutually exclusive with showFirstRunCling, and only runs if this Launcher
     90      * package was not preinstalled and there exists a db to migrate from.
     91      */
     92     public void showMigrationCling() {
     93         mIsVisible = true;
     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         mIsVisible = true;
    121         ViewGroup root = (ViewGroup) mLauncher.findViewById(R.id.launcher);
    122         View cling = mInflater.inflate(R.layout.longpress_cling, root, false);
    123 
    124         cling.setOnLongClickListener(new OnLongClickListener() {
    125 
    126             @Override
    127             public boolean onLongClick(View v) {
    128                 mLauncher.showOverviewMode(true);
    129                 dismissLongPressCling();
    130                 return true;
    131             }
    132         });
    133 
    134         final ViewGroup content = (ViewGroup) cling.findViewById(R.id.cling_content);
    135         mInflater.inflate(showWelcome ? R.layout.longpress_cling_welcome_content
    136                 : R.layout.longpress_cling_content, content);
    137         content.findViewById(R.id.cling_dismiss_longpress_info).setOnClickListener(this);
    138 
    139         if (TAG_CROP_TOP_AND_SIDES.equals(content.getTag())) {
    140             Drawable bg = new BorderCropDrawable(mLauncher.getResources().getDrawable(R.drawable.cling_bg),
    141                     true, true, true, false);
    142             content.setBackground(bg);
    143         }
    144 
    145         root.addView(cling);
    146 
    147         if (showWelcome) {
    148             // This is the first cling being shown. No need to animate.
    149             return;
    150         }
    151 
    152         // Animate
    153         content.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    154 
    155             @Override
    156             public void onGlobalLayout() {
    157                 content.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    158 
    159                 ObjectAnimator anim;
    160                 if (TAG_CROP_TOP_AND_SIDES.equals(content.getTag())) {
    161                     content.setTranslationY(-content.getMeasuredHeight());
    162                     anim = LauncherAnimUtils.ofFloat(content, "translationY", 0);
    163                 } else {
    164                     content.setScaleX(0);
    165                     content.setScaleY(0);
    166                     PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1);
    167                     PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1);
    168                     anim = LauncherAnimUtils.ofPropertyValuesHolder(content, scaleX, scaleY);
    169                 }
    170 
    171                 anim.setDuration(SHOW_CLING_DURATION);
    172                 anim.setInterpolator(new LogDecelerateInterpolator(100, 0));
    173                 anim.start();
    174             }
    175         });
    176     }
    177 
    178     @Thunk void dismissLongPressCling() {
    179         Runnable dismissCb = new Runnable() {
    180             public void run() {
    181                 dismissCling(mLauncher.findViewById(R.id.longpress_cling), null,
    182                         WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
    183             }
    184         };
    185         mLauncher.getWorkspace().post(dismissCb);
    186     }
    187 
    188     /** Hides the specified Cling */
    189     @Thunk void dismissCling(final View cling, final Runnable postAnimationCb,
    190                               final String flag, int duration) {
    191         // To catch cases where siblings of top-level views are made invisible, just check whether
    192         // the cling is directly set to GONE before dismissing it.
    193         if (cling != null && cling.getVisibility() != View.GONE) {
    194             final Runnable cleanUpClingCb = new Runnable() {
    195                 public void run() {
    196                     cling.setVisibility(View.GONE);
    197                     mLauncher.getSharedPrefs().edit()
    198                         .putBoolean(flag, true)
    199                         .apply();
    200                     mIsVisible = false;
    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);
    210             }
    211         }
    212     }
    213 
    214     public boolean isVisible() {
    215         return mIsVisible;
    216     }
    217 
    218     /** Returns whether the clings are enabled or should be shown */
    219     @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    220     private boolean areClingsEnabled() {
    221         // disable clings when running in a test harness
    222         if(ActivityManager.isRunningInTestHarness()) return false;
    223 
    224         // Disable clings for accessibility when explore by touch is enabled
    225         final AccessibilityManager a11yManager = (AccessibilityManager) mLauncher.getSystemService(
    226                 Launcher.ACCESSIBILITY_SERVICE);
    227         if (a11yManager.isTouchExplorationEnabled()) {
    228             return false;
    229         }
    230 
    231         // Restricted secondary users (child mode) will potentially have very few apps
    232         // seeded when they start up for the first time. Clings won't work well with that
    233         if (Utilities.ATLEAST_JB_MR2) {
    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         }
    240         if (Settings.Secure.getInt(mLauncher.getContentResolver(), SKIP_FIRST_USE_HINTS, 0)
    241                 == 1) {
    242             return false;
    243         }
    244         return true;
    245     }
    246 
    247     public boolean shouldShowFirstRunOrMigrationClings() {
    248         SharedPreferences sharedPrefs = mLauncher.getSharedPrefs();
    249         return areClingsEnabled() &&
    250             !sharedPrefs.getBoolean(WORKSPACE_CLING_DISMISSED_KEY, false) &&
    251             !sharedPrefs.getBoolean(MIGRATION_CLING_DISMISSED_KEY, false);
    252     }
    253 
    254     public static void markFirstRunClingDismissed(Context ctx) {
    255         Utilities.getPrefs(ctx).edit()
    256                 .putBoolean(WORKSPACE_CLING_DISMISSED_KEY, true)
    257                 .apply();
    258     }
    259 }
    260