Home | History | Annotate | Download | only in shortcuts
      1 /*
      2  * Copyright (C) 2016 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.dialer.shortcuts;
     18 
     19 import android.annotation.TargetApi;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.pm.ShortcutInfo;
     23 import android.os.Build.VERSION_CODES;
     24 import android.support.annotation.NonNull;
     25 import android.support.annotation.WorkerThread;
     26 import com.android.dialer.common.Assert;
     27 import java.util.ArrayList;
     28 import java.util.List;
     29 import java.util.Map;
     30 
     31 /**
     32  * Creates {@link ShortcutInfo} objects (which are required by shortcut manager system service) from
     33  * {@link DialerShortcut} objects (which are package-private convenience data structures).
     34  *
     35  * <p>The main work this factory does is create shortcut intents. It also delegates to the {@link
     36  * IconFactory} to create icons.
     37  */
     38 @TargetApi(VERSION_CODES.N_MR1) // Shortcuts introduced in N MR1
     39 final class ShortcutInfoFactory {
     40 
     41   /** Key for the contact ID extra (a long) stored as part of the shortcut intent. */
     42   static final String EXTRA_CONTACT_ID = "contactId";
     43 
     44   private final Context context;
     45   private final IconFactory iconFactory;
     46 
     47   ShortcutInfoFactory(@NonNull Context context, IconFactory iconFactory) {
     48     this.context = context;
     49     this.iconFactory = iconFactory;
     50   }
     51 
     52   /**
     53    * Builds a list {@link ShortcutInfo} objects from the provided collection of {@link
     54    * DialerShortcut} objects. This primarily means setting the intent and adding the icon, which
     55    * {@link DialerShortcut} objects do not hold.
     56    */
     57   @WorkerThread
     58   @NonNull
     59   List<ShortcutInfo> buildShortcutInfos(@NonNull Map<String, DialerShortcut> shortcutsById) {
     60     Assert.isWorkerThread();
     61     List<ShortcutInfo> shortcuts = new ArrayList<>(shortcutsById.size());
     62     for (DialerShortcut shortcut : shortcutsById.values()) {
     63       Intent intent = new Intent();
     64       intent.setClassName(context, "com.android.dialer.shortcuts.CallContactActivity");
     65       intent.setData(shortcut.getLookupUri());
     66       intent.setAction("com.android.dialer.shortcuts.CALL_CONTACT");
     67       intent.putExtra(EXTRA_CONTACT_ID, shortcut.getContactId());
     68 
     69       ShortcutInfo.Builder shortcutInfo =
     70           new ShortcutInfo.Builder(context, shortcut.getShortcutId())
     71               .setIntent(intent)
     72               .setShortLabel(shortcut.getShortLabel())
     73               .setLongLabel(shortcut.getLongLabel())
     74               .setIcon(iconFactory.create(shortcut));
     75 
     76       if (shortcut.getRank() != DialerShortcut.NO_RANK) {
     77         shortcutInfo.setRank(shortcut.getRank());
     78       }
     79       shortcuts.add(shortcutInfo.build());
     80     }
     81     return shortcuts;
     82   }
     83 
     84   /**
     85    * Creates a copy of the provided {@link ShortcutInfo} but with an updated icon fetched from
     86    * contacts provider.
     87    */
     88   @WorkerThread
     89   @NonNull
     90   ShortcutInfo withUpdatedIcon(ShortcutInfo info) {
     91     Assert.isWorkerThread();
     92     return new ShortcutInfo.Builder(context, info.getId())
     93         .setIntent(info.getIntent())
     94         .setShortLabel(info.getShortLabel())
     95         .setLongLabel(info.getLongLabel())
     96         .setRank(info.getRank())
     97         .setIcon(iconFactory.create(info))
     98         .build();
     99   }
    100 }
    101