Home | History | Annotate | Download | only in packages
      1 /*
      2  * Copyright (C) 2017 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 package android.content.pm.cts.shortcutmanager.packages;
     17 
     18 import android.app.Activity;
     19 import android.content.pm.LauncherApps;
     20 import android.content.pm.LauncherApps.PinItemRequest;
     21 import android.content.pm.ShortcutInfo;
     22 import android.content.pm.cts.shortcutmanager.common.Constants;
     23 import android.content.pm.cts.shortcutmanager.common.ReplyUtil;
     24 import android.graphics.drawable.Drawable;
     25 import android.os.Bundle;
     26 import android.util.DisplayMetrics;
     27 import android.util.Log;
     28 
     29 import java.util.Objects;
     30 
     31 /**
     32  * Activity that receives a "pin shortcut" request, and accepts automatically.
     33  */
     34 public class ShortcutConfirmPin extends Activity {
     35     private static final String TAG = "ShortcutConfirmPin";
     36 
     37     @Override
     38     protected void onCreate(Bundle savedInstanceState) {
     39         super.onCreate(savedInstanceState);
     40 
     41         Log.i(TAG, "ShortcutConfirmPin.onCreate");
     42 
     43         String replyAction = null;
     44         try {
     45             final LauncherApps launcherApps = getSystemService(LauncherApps.class);
     46             final PinItemRequest request = launcherApps.getPinItemRequest(getIntent());
     47 
     48             // This really must be non-null; otherwise we can't send a reply.
     49             final ShortcutInfo shortcut = request.getShortcutInfo();
     50             if (shortcut == null) {
     51                 Log.e(TAG, "request.getShortcutInfo() NOT expected to be NULL");
     52                 return;
     53             }
     54 
     55             replyAction = shortcut.getExtras().getString(Constants.EXTRA_REPLY_ACTION);
     56 
     57             if (!request.isValid()) {
     58                 ReplyUtil.sendReply(this, replyAction, "request.isValid() expected to be TRUE");
     59                 return;
     60             }
     61             if (request.getRequestType() != PinItemRequest.REQUEST_TYPE_SHORTCUT) {
     62                 ReplyUtil.sendReply(this, replyAction,
     63                         "request.getRequestType() expected to be REQUEST_TYPE_SHORTCUT");
     64                 return;
     65             }
     66 
     67             if (shortcut.getExtras().getBoolean(Constants.IGNORE)) {
     68                 // Send a reply so that the caller can tell if the request has been sent,
     69                 // and ignored.
     70                 ReplyUtil.sendReply(this, replyAction, Constants.REQUEST_IGNORED_MESSAGE);
     71                 return;
     72             }
     73 
     74             // Check the shortcut's fields.
     75             final boolean expectPinned = shortcut.getExtras().getBoolean(Constants.ALREADY_PINNED);
     76             if (shortcut.isPinned() != expectPinned) {
     77                 ReplyUtil.sendReply(this, replyAction, "isPinned() expected to be " + expectPinned);
     78                 return;
     79             }
     80 
     81             final String expectLabel = shortcut.getExtras().getString(Constants.LABEL);
     82             if (!Objects.equals(expectLabel, shortcut.getShortLabel())) {
     83                 ReplyUtil.sendReply(this, replyAction,
     84                         "getShortLabel() expected to be '" + expectLabel + "', but was '"
     85                         + shortcut.getShortLabel() + "'");
     86                 return;
     87             }
     88             final Drawable icon = launcherApps.getShortcutBadgedIconDrawable(
     89                     shortcut, DisplayMetrics.DENSITY_DEFAULT);
     90             if (shortcut.getExtras().getBoolean(Constants.HAS_ICON)) {
     91                 // Send a reply so that the caller can tell if the request has been sent,
     92                 // and ignored.
     93                 if (icon == null) {
     94                     ReplyUtil.sendReply(this, replyAction, "Expected to have icon");
     95                     return;
     96                 }
     97             } else {
     98                 if (icon != null) {
     99                     ReplyUtil.sendReply(this, replyAction, "Not expected to have icon");
    100                     return;
    101                 }
    102             }
    103 
    104             request.accept();
    105             if (request.isValid()) {
    106                 ReplyUtil.sendReply(this, replyAction,
    107                         "request.isValid() expected to be FALSE after accept()");
    108                 return;
    109             }
    110             ReplyUtil.sendSuccessReply(this, replyAction);
    111             Log.e(TAG, "Sent reply");
    112         } catch (Exception e) {
    113             Log.e(TAG, "Caught exception", e);
    114             if (replyAction != null) {
    115                 ReplyUtil.sendReply(this, replyAction, "Caught exception: " + e);
    116             }
    117         } finally {
    118             finish();
    119         }
    120     }
    121 }
    122