Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 08d87ee3 authored by Gary Mai's avatar Gary Mai
Browse files

Adopt O shortcut changes for Intent.ACTION_CREATE_SHORTCUT

If we're on O, use ShortcutManager's .createShortcutResultIntent
and return that to the launcher.
Move ShortcutIntentBuilder out of list package since it doesn't
really make sense in there. Also allows us to keep DynamicShortcuts'
methods package private.

Test: Manually verify shortcuts created by long pressing on the
home screen (Contacts 1x1 widgets) are the newer app shortcuts on O
and are legacy shortcuts on pre-O (L, M, and N).

Bug: 36032908
Change-Id: Ic78c21daf223b59b45cbc98ceea2726fc29c055c
parent fdb828ff
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -185,6 +185,8 @@
            android:uiOptions="splitActionBarWhenNarrow"
            android:windowSoftInputMode="adjustResize"
            android:visibleToInstantApps="true">
            <meta-data android:name="android.app.shortcuts.new_config"
                android:value="true" />
            <intent-filter>
                <action android:name="android.intent.action.INSERT_OR_EDIT"/>
                <category android:name="android.intent.category.DEFAULT"/>
+31 −10
Original line number Diff line number Diff line
@@ -275,18 +275,25 @@ public class DynamicShortcuts {
                .setDisabledMessage(mContext.getString(R.string.dynamic_shortcut_disabled_message))
                .setExtras(extras);

        if (displayName.length() < mLongLabelMaxLength) {
            builder.setLongLabel(displayName);
        } else {
            builder.setLongLabel(displayName.substring(0, mLongLabelMaxLength - 1).trim() + "…");
        setLabel(builder, displayName);
        return builder;
    }

        if (displayName.length() < mShortLabelMaxLength) {
            builder.setShortLabel(displayName);
        } else {
            builder.setShortLabel(displayName.substring(0, mShortLabelMaxLength - 1).trim() + "…");
    @VisibleForTesting
    ShortcutInfo getActionShortcutInfo(String id, String label, Intent action, Icon icon) {
        if (id == null || label == null) {
            return null;
        }
        return builder;
        final PersistableBundle extras = new PersistableBundle();
        extras.putInt(EXTRA_SHORTCUT_TYPE, SHORTCUT_TYPE_CONTACT_URI);

        final ShortcutInfo.Builder builder = new ShortcutInfo.Builder(mContext, id)
                .setIntent(action)
                .setIcon(icon)
                .setDisabledMessage(mContext.getString(R.string.dynamic_shortcut_disabled_message));

        setLabel(builder, label);
        return builder.build();
    }

    public ShortcutInfo getQuickContactShortcutInfo(long id, String lookupKey, String displayName) {
@@ -295,6 +302,20 @@ public class DynamicShortcuts {
        return builder.build();
    }

    private void setLabel(ShortcutInfo.Builder builder, String label) {
        if (label.length() < mLongLabelMaxLength) {
            builder.setLongLabel(label);
        } else {
            builder.setLongLabel(label.substring(0, mLongLabelMaxLength - 1).trim() + "…");
        }

        if (label.length() < mShortLabelMaxLength) {
            builder.setShortLabel(label);
        } else {
            builder.setShortLabel(label.substring(0, mShortLabelMaxLength - 1).trim() + "…");
        }
    }

    private void addIconForContact(Cursor cursor, ShortcutInfo.Builder builder) {
        final long id = cursor.getLong(0);
        final String lookupKey = cursor.getString(1);
+78 −49
Original line number Diff line number Diff line
@@ -13,12 +13,14 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.contacts.list;
package com.android.contacts;

import android.app.ActivityManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
@@ -29,6 +31,7 @@ import android.graphics.Paint.FontMetricsInt;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.ContactsContract.CommonDataKinds.Phone;
@@ -37,18 +40,20 @@ import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v4.os.BuildCompat;
import android.telecom.PhoneAccount;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.TextUtils.TruncateAt;

import com.android.contacts.ContactPhotoManager;
import com.android.contacts.ContactPhotoManager.DefaultImageRequest;
import com.android.contacts.ContactsUtils;
import com.android.contacts.R;
import com.android.contacts.util.BitmapUtil;
import com.android.contacts.util.ImplicitIntentsUtil;

import com.google.common.collect.Lists;

import java.util.List;

/**
 * Constructs shortcut intents.
 */
@@ -264,22 +269,30 @@ public class ShortcutIntentBuilder {

    private void createContactShortcutIntent(Uri contactUri, String contentType, String displayName,
            String lookupKey, byte[] bitmapData) {
        Drawable drawable = getPhotoDrawable(bitmapData, displayName, lookupKey);
        final Drawable drawable = getPhotoDrawable(bitmapData, displayName, lookupKey);
        if (TextUtils.isEmpty(displayName)) {
            displayName = mContext.getResources().getString(R.string.missing_name);
        }

        final Intent shortcutIntent = ImplicitIntentsUtil.getIntentForQuickContactLauncherShortcut(
                mContext, contactUri);

        final Bitmap icon = generateQuickContactIcon(drawable);

        Intent intent = new Intent();
        Intent intent = null;
        if (BuildCompat.isAtLeastO()) {
            final ShortcutManager sm = (ShortcutManager)
                    mContext.getSystemService(Context.SHORTCUT_SERVICE);
            final DynamicShortcuts dynamicShortcuts = new DynamicShortcuts(mContext);
            final ShortcutInfo shortcutInfo = dynamicShortcuts.getActionShortcutInfo(
                    lookupKey, displayName, shortcutIntent, Icon.createWithBitmap(icon));
            intent = sm.createShortcutResultIntent(shortcutInfo);
        }

        intent = intent == null ? new Intent() : intent;
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        if (TextUtils.isEmpty(displayName)) {
            intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, mContext.getResources().getString(
                    R.string.missing_name));
        } else {
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, displayName);
        }

        mListener.onShortcutIntentCreated(contactUri, intent);
    }
@@ -287,31 +300,42 @@ public class ShortcutIntentBuilder {
    private void createPhoneNumberShortcutIntent(Uri uri, String displayName, String lookupKey,
            byte[] bitmapData, String phoneNumber, int phoneType, String phoneLabel,
            String shortcutAction) {
        Drawable drawable = getPhotoDrawable(bitmapData, displayName, lookupKey);
        final Drawable drawable = getPhotoDrawable(bitmapData, displayName, lookupKey);
        final Bitmap icon;
        final Uri phoneUri;

        if (TextUtils.isEmpty(displayName)) {
            displayName = mContext.getResources().getString(R.string.missing_name);
        }

        Bitmap bitmap;
        Uri phoneUri;
        if (Intent.ACTION_CALL.equals(shortcutAction)) {
            // Make the URI a direct tel: URI so that it will always continue to work
            phoneUri = Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null);
            bitmap = generatePhoneNumberIcon(drawable, phoneType, phoneLabel,
            icon = generatePhoneNumberIcon(drawable, phoneType, phoneLabel,
                    R.drawable.quantum_ic_phone_vd_theme_24);
        } else {
            phoneUri = Uri.fromParts(ContactsUtils.SCHEME_SMSTO, phoneNumber, null);
            bitmap = generatePhoneNumberIcon(drawable, phoneType, phoneLabel,
            icon = generatePhoneNumberIcon(drawable, phoneType, phoneLabel,
                    R.drawable.quantum_ic_message_vd_theme_24);
        }

        Intent shortcutIntent = new Intent(shortcutAction, phoneUri);
        final Intent shortcutIntent = new Intent(shortcutAction, phoneUri);
        shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        Intent intent = new Intent();
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);

        if (TextUtils.isEmpty(displayName)) {
            displayName = mContext.getResources().getString(R.string.missing_name);
        Intent intent = null;
        if (BuildCompat.isAtLeastO()) {
            final ShortcutManager sm = (ShortcutManager)
                    mContext.getSystemService(Context.SHORTCUT_SERVICE);
            final String id = shortcutAction + lookupKey;
            final DynamicShortcuts dynamicShortcuts = new DynamicShortcuts(mContext);
            final ShortcutInfo shortcutInfo = dynamicShortcuts.getActionShortcutInfo(
                    id, displayName, shortcutIntent, Icon.createWithBitmap(icon));
            intent = sm.createShortcutResultIntent(shortcutInfo);
        }

        intent = intent == null ? new Intent() : intent;
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        if (TextUtils.equals(shortcutAction, Intent.ACTION_CALL)) {
            intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
                    mContext.getResources().getString(R.string.call_by_shortcut, displayName));
@@ -371,11 +395,15 @@ public class ShortcutIntentBuilder {
        photoPaint.setFilterBitmap(true);
        Rect dst = new Rect(0, 0, mIconSize, mIconSize);

        // Create the overlay if we're pre-O. O created shortcuts have the app badge which overlaps
        // the type overlay.
        if (!BuildCompat.isAtLeastO()) {
            // Create an overlay for the phone number type
            CharSequence overlay = Phone.getTypeLabel(r, phoneType, phoneLabel);

            if (overlay != null) {
            TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
                TextPaint textPaint = new TextPaint(
                        Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
                textPaint.setTextSize(r.getDimension(R.dimen.shortcut_overlay_text_size));
                textPaint.setColor(r.getColor(R.color.textColorIconOverlay));
                textPaint.setShadowLayer(4f, 0, 2f, r.getColor(R.color.textColorIconOverlayShadow));
@@ -397,6 +425,7 @@ public class ShortcutIntentBuilder {
                canvas.drawText(overlay, 0, overlay.length(), (mIconSize - textWidth) / 2, mIconSize
                        - fmi.descent - textPadding, textPaint);
            }
        }

        // Draw the phone action icon as an overlay
        Rect src = new Rect(0, 0, phoneIcon.getWidth(), phoneIcon.getHeight());
+2 −1
Original line number Diff line number Diff line
@@ -24,7 +24,8 @@ import android.view.ViewGroup;
import android.widget.AdapterView;

import com.android.contacts.R;
import com.android.contacts.list.ShortcutIntentBuilder.OnShortcutIntentCreatedListener;
import com.android.contacts.ShortcutIntentBuilder;
import com.android.contacts.ShortcutIntentBuilder.OnShortcutIntentCreatedListener;

/**
 * Fragment for the contact list used for browsing contacts (as compared to
+2 −1
Original line number Diff line number Diff line
@@ -28,7 +28,8 @@ import android.view.View;
import android.view.ViewGroup;

import com.android.contacts.R;
import com.android.contacts.list.ShortcutIntentBuilder.OnShortcutIntentCreatedListener;
import com.android.contacts.ShortcutIntentBuilder;
import com.android.contacts.ShortcutIntentBuilder.OnShortcutIntentCreatedListener;

/**
 * Fragment containing a phone number list for picking.
Loading