1 /* 2 * Copyright 2018 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 androidx.sharetarget.testapp; 18 19 import android.app.Activity; 20 import android.content.Intent; 21 import android.os.Bundle; 22 import android.view.View; 23 24 import androidx.core.app.Person; 25 import androidx.core.content.pm.ShortcutInfoCompat; 26 import androidx.core.content.pm.ShortcutManagerCompat; 27 import androidx.core.graphics.drawable.IconCompat; 28 29 import java.util.ArrayList; 30 import java.util.HashSet; 31 import java.util.Set; 32 33 /** 34 * Start activity for the Direct Share Sample application. This class manages the UI which allows a 35 * user to push or remove direct share targets. 36 */ 37 public class MainActivity extends Activity { 38 39 private static final String CATEGORY_TEXT_SHARE_TARGET = 40 "androidx.sharetarget.testapp.category.TEXT_SHARE_TARGET"; 41 private static final String CATEGORY_OTHER_TEXT_SHARE_TARGET = 42 "androidx.sharetarget.testapp.category.OTHER_TEXT_SHARE_TARGET"; 43 44 @Override onCreate(Bundle savedInstanceState)45 protected void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 setContentView(R.layout.activity_main); 48 49 findViewById(R.id.push_targets).setOnClickListener(mOnClickListener); 50 findViewById(R.id.remove_targets).setOnClickListener(mOnClickListener); 51 } 52 53 private View.OnClickListener mOnClickListener = new View.OnClickListener() { 54 @Override 55 public void onClick(View view) { 56 int id = view.getId(); 57 if (id == R.id.push_targets) { 58 pushDirectShareTargets(); 59 } else if (id == R.id.remove_targets) { 60 removeAllDirectShareTargets(); 61 } 62 } 63 }; 64 pushDirectShareTargets()65 private void pushDirectShareTargets() { 66 Intent intent = new Intent(Intent.ACTION_DEFAULT); 67 68 Set<String> categories1 = new HashSet<>(); 69 categories1.add(CATEGORY_TEXT_SHARE_TARGET); 70 71 Set<String> categories2 = new HashSet<>(); 72 categories2.add(CATEGORY_OTHER_TEXT_SHARE_TARGET); 73 74 ArrayList<ShortcutInfoCompat> shortcuts = new ArrayList<>(); 75 76 shortcuts.add(new ShortcutInfoCompat.Builder(this, "Person_One_ID") 77 .setShortLabel("Person_One") 78 .setIcon(IconCompat.createWithResource(this, R.mipmap.logo_avatar)) 79 .setIntent(intent) 80 .setLongLived() 81 .setPerson(new Person.Builder().build()) 82 .setCategories(categories1) 83 .setRank(2) 84 .build()); 85 shortcuts.add(new ShortcutInfoCompat.Builder(this, "Person_Two_ID") 86 .setShortLabel("Person_Two") 87 .setIcon(IconCompat.createWithResource(this, R.mipmap.logo_avatar)) 88 .setIntent(intent) 89 .setLongLived() 90 .setPerson(new Person.Builder().build()) 91 .setCategories(categories2) 92 .setRank(1) 93 .build()); 94 95 ShortcutManagerCompat.addDynamicShortcuts(this, shortcuts); 96 } 97 removeAllDirectShareTargets()98 private void removeAllDirectShareTargets() { 99 ShortcutManagerCompat.removeAllDynamicShortcuts(this); 100 } 101 } 102