Home | History | Annotate | Download | only in com.example.android.directshare
      1 /*
      2  * Copyright (C) 2015 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.example.android.directshare;
     18 
     19 import android.content.ComponentName;
     20 import android.content.IntentFilter;
     21 import android.graphics.drawable.Icon;
     22 import android.os.Bundle;
     23 import android.service.chooser.ChooserTarget;
     24 import android.service.chooser.ChooserTargetService;
     25 
     26 import java.util.ArrayList;
     27 import java.util.List;
     28 
     29 /**
     30  * Provides the Direct Share items to the system.
     31  */
     32 public class SampleChooserTargetService extends ChooserTargetService {
     33 
     34     @Override
     35     public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName,
     36                                                    IntentFilter matchedFilter) {
     37         ComponentName componentName = new ComponentName(getPackageName(),
     38                 SendMessageActivity.class.getCanonicalName());
     39         // The list of Direct Share items. The system will show the items the way they are sorted
     40         // in this list.
     41         ArrayList<ChooserTarget> targets = new ArrayList<>();
     42         for (int i = 0; i < Contact.CONTACTS.length; ++i) {
     43             Contact contact = Contact.byId(i);
     44             Bundle extras = new Bundle();
     45             extras.putInt(Contact.ID, i);
     46             targets.add(new ChooserTarget(
     47                     // The name of this target.
     48                     contact.getName(),
     49                     // The icon to represent this target.
     50                     Icon.createWithResource(this, contact.getIcon()),
     51                     // The ranking score for this target (0.0-1.0); the system will omit items with
     52                     // low scores when there are too many Direct Share items.
     53                     0.5f,
     54                     // The name of the component to be launched if this target is chosen.
     55                     componentName,
     56                     // The extra values here will be merged into the Intent when this target is
     57                     // chosen.
     58                     extras));
     59         }
     60         return targets;
     61     }
     62 
     63 }
     64