1*90c8c64dSAndroid Build Coastguard Worker /*
2*90c8c64dSAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*90c8c64dSAndroid Build Coastguard Worker  *
4*90c8c64dSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*90c8c64dSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*90c8c64dSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*90c8c64dSAndroid Build Coastguard Worker  *
8*90c8c64dSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*90c8c64dSAndroid Build Coastguard Worker  *
10*90c8c64dSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*90c8c64dSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*90c8c64dSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*90c8c64dSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*90c8c64dSAndroid Build Coastguard Worker  * limitations under the License.
15*90c8c64dSAndroid Build Coastguard Worker  */
16*90c8c64dSAndroid Build Coastguard Worker 
17*90c8c64dSAndroid Build Coastguard Worker package com.example.android.directshare;
18*90c8c64dSAndroid Build Coastguard Worker 
19*90c8c64dSAndroid Build Coastguard Worker import android.content.ComponentName;
20*90c8c64dSAndroid Build Coastguard Worker import android.content.IntentFilter;
21*90c8c64dSAndroid Build Coastguard Worker import android.graphics.drawable.Icon;
22*90c8c64dSAndroid Build Coastguard Worker import android.os.Bundle;
23*90c8c64dSAndroid Build Coastguard Worker import android.service.chooser.ChooserTarget;
24*90c8c64dSAndroid Build Coastguard Worker import android.service.chooser.ChooserTargetService;
25*90c8c64dSAndroid Build Coastguard Worker 
26*90c8c64dSAndroid Build Coastguard Worker import java.util.ArrayList;
27*90c8c64dSAndroid Build Coastguard Worker import java.util.List;
28*90c8c64dSAndroid Build Coastguard Worker 
29*90c8c64dSAndroid Build Coastguard Worker /**
30*90c8c64dSAndroid Build Coastguard Worker  * Provides the Direct Share items to the system.
31*90c8c64dSAndroid Build Coastguard Worker  */
32*90c8c64dSAndroid Build Coastguard Worker public class SampleChooserTargetService extends ChooserTargetService {
33*90c8c64dSAndroid Build Coastguard Worker 
34*90c8c64dSAndroid Build Coastguard Worker     @Override
onGetChooserTargets(ComponentName targetActivityName, IntentFilter matchedFilter)35*90c8c64dSAndroid Build Coastguard Worker     public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName,
36*90c8c64dSAndroid Build Coastguard Worker                                                    IntentFilter matchedFilter) {
37*90c8c64dSAndroid Build Coastguard Worker         ComponentName componentName = new ComponentName(getPackageName(),
38*90c8c64dSAndroid Build Coastguard Worker                 SendMessageActivity.class.getCanonicalName());
39*90c8c64dSAndroid Build Coastguard Worker         // The list of Direct Share items. The system will show the items the way they are sorted
40*90c8c64dSAndroid Build Coastguard Worker         // in this list.
41*90c8c64dSAndroid Build Coastguard Worker         ArrayList<ChooserTarget> targets = new ArrayList<>();
42*90c8c64dSAndroid Build Coastguard Worker         for (int i = 0; i < Contact.CONTACTS.length; ++i) {
43*90c8c64dSAndroid Build Coastguard Worker             Contact contact = Contact.byId(i);
44*90c8c64dSAndroid Build Coastguard Worker             Bundle extras = new Bundle();
45*90c8c64dSAndroid Build Coastguard Worker             extras.putInt(Contact.ID, i);
46*90c8c64dSAndroid Build Coastguard Worker             targets.add(new ChooserTarget(
47*90c8c64dSAndroid Build Coastguard Worker                     // The name of this target.
48*90c8c64dSAndroid Build Coastguard Worker                     contact.getName(),
49*90c8c64dSAndroid Build Coastguard Worker                     // The icon to represent this target.
50*90c8c64dSAndroid Build Coastguard Worker                     Icon.createWithResource(this, contact.getIcon()),
51*90c8c64dSAndroid Build Coastguard Worker                     // The ranking score for this target (0.0-1.0); the system will omit items with
52*90c8c64dSAndroid Build Coastguard Worker                     // low scores when there are too many Direct Share items.
53*90c8c64dSAndroid Build Coastguard Worker                     0.5f,
54*90c8c64dSAndroid Build Coastguard Worker                     // The name of the component to be launched if this target is chosen.
55*90c8c64dSAndroid Build Coastguard Worker                     componentName,
56*90c8c64dSAndroid Build Coastguard Worker                     // The extra values here will be merged into the Intent when this target is
57*90c8c64dSAndroid Build Coastguard Worker                     // chosen.
58*90c8c64dSAndroid Build Coastguard Worker                     extras));
59*90c8c64dSAndroid Build Coastguard Worker         }
60*90c8c64dSAndroid Build Coastguard Worker         return targets;
61*90c8c64dSAndroid Build Coastguard Worker     }
62*90c8c64dSAndroid Build Coastguard Worker 
63*90c8c64dSAndroid Build Coastguard Worker }
64