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 17 package com.android.launcher3.compat; 18 19 import android.appwidget.AppWidgetProviderInfo; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.pm.ShortcutInfo; 23 import android.os.Bundle; 24 import android.os.Parcel; 25 import android.os.Parcelable; 26 27 import com.android.launcher3.Utilities; 28 29 /** 30 * A wrapper around platform implementation of PinItemRequestCompat until the 31 * updated SDK is available. 32 */ 33 public class PinItemRequestCompat implements Parcelable { 34 35 public static final String EXTRA_PIN_ITEM_REQUEST = "android.content.pm.extra.PIN_ITEM_REQUEST"; 36 37 public static final int REQUEST_TYPE_SHORTCUT = 1; 38 public static final int REQUEST_TYPE_APPWIDGET = 2; 39 40 private final Parcelable mObject; 41 PinItemRequestCompat(Parcelable object)42 private PinItemRequestCompat(Parcelable object) { 43 mObject = object; 44 } 45 getRequestType()46 public int getRequestType() { 47 return (Integer) invokeMethod("getRequestType"); 48 } 49 getShortcutInfo()50 public ShortcutInfo getShortcutInfo() { 51 return (ShortcutInfo) invokeMethod("getShortcutInfo"); 52 } 53 getAppWidgetProviderInfo(Context context)54 public AppWidgetProviderInfo getAppWidgetProviderInfo(Context context) { 55 try { 56 return (AppWidgetProviderInfo) mObject.getClass() 57 .getDeclaredMethod("getAppWidgetProviderInfo", Context.class) 58 .invoke(mObject, context); 59 } catch (Exception e) { 60 throw new RuntimeException(e); 61 } 62 } 63 isValid()64 public boolean isValid() { 65 return (Boolean) invokeMethod("isValid"); 66 } 67 accept()68 public boolean accept() { 69 return (Boolean) invokeMethod("accept"); 70 } 71 accept(Bundle options)72 public boolean accept(Bundle options) { 73 try { 74 return (Boolean) mObject.getClass().getDeclaredMethod("accept", Bundle.class) 75 .invoke(mObject, options); 76 } catch (Exception e) { 77 throw new RuntimeException(e); 78 } 79 } 80 getExtras()81 public Bundle getExtras() { 82 try { 83 return (Bundle) mObject.getClass().getDeclaredMethod("getExtras").invoke(mObject); 84 } catch (Exception e) { 85 return null; 86 } 87 } 88 invokeMethod(String methodName)89 private Object invokeMethod(String methodName) { 90 try { 91 return mObject.getClass().getDeclaredMethod(methodName).invoke(mObject); 92 } catch (Exception e) { 93 throw new RuntimeException(e); 94 } 95 } 96 97 @Override describeContents()98 public int describeContents() { 99 return 0; 100 } 101 102 @Override writeToParcel(Parcel parcel, int i)103 public void writeToParcel(Parcel parcel, int i) { 104 parcel.writeParcelable(mObject, i); 105 } 106 107 public static final Parcelable.Creator<PinItemRequestCompat> CREATOR = 108 new Parcelable.Creator<PinItemRequestCompat>() { 109 public PinItemRequestCompat createFromParcel(Parcel source) { 110 Parcelable object = source.readParcelable(null); 111 return new PinItemRequestCompat(object); 112 } 113 114 public PinItemRequestCompat[] newArray(int size) { 115 return new PinItemRequestCompat[size]; 116 } 117 }; 118 getPinItemRequest(Intent intent)119 public static PinItemRequestCompat getPinItemRequest(Intent intent) { 120 if (!Utilities.isAtLeastO()) { 121 return null; 122 } 123 Parcelable extra = intent.getParcelableExtra(EXTRA_PIN_ITEM_REQUEST); 124 return extra == null ? null : new PinItemRequestCompat(extra); 125 } 126 } 127