1 /*
2  * Copyright (C) 2022 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.server.wm.flicker.testapp;
18 
19 
20 import static android.Manifest.permission.POST_NOTIFICATIONS;
21 import static android.content.pm.PackageManager.PERMISSION_GRANTED;
22 
23 import android.app.Activity;
24 import android.app.Person;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.pm.ShortcutInfo;
28 import android.content.pm.ShortcutManager;
29 import android.graphics.drawable.Icon;
30 import android.os.Build;
31 import android.os.Bundle;
32 import android.view.View;
33 
34 import java.util.Arrays;
35 
36 public class LaunchBubbleActivity extends Activity {
37 
38     private BubbleHelper mBubbleHelper;
39 
40     @Override
onCreate(Bundle savedInstanceState)41     protected void onCreate(Bundle savedInstanceState) {
42         super.onCreate(savedInstanceState);
43 
44         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
45                 && checkSelfPermission(POST_NOTIFICATIONS) != PERMISSION_GRANTED) {
46             // POST_NOTIFICATIONS permission required for notification post sdk 33.
47             requestPermissions(new String[] { POST_NOTIFICATIONS }, 0);
48         }
49 
50         addInboxShortcut(getApplicationContext());
51         mBubbleHelper = BubbleHelper.getInstance(this);
52         setContentView(R.layout.activity_main);
53         findViewById(R.id.button_create).setOnClickListener(this::add);
54         findViewById(R.id.button_cancel).setOnClickListener(this::cancel);
55         findViewById(R.id.button_cancel_all).setOnClickListener(this::cancelAll);
56     }
57 
add(View v)58     private void add(View v) {
59         mBubbleHelper.addNewBubble(false /* autoExpand */, false /* suppressNotif */);
60     }
61 
cancel(View v)62     private void cancel(View v) {
63         mBubbleHelper.cancelLast();
64     }
65 
cancelAll(View v)66     private void cancelAll(View v) {
67         mBubbleHelper.cancelAll();
68     }
69 
addInboxShortcut(Context context)70     private void addInboxShortcut(Context context) {
71         Icon icon = Icon.createWithResource(this, R.drawable.bg);
72         Person[] persons = new Person[4];
73         for (int i = 0; i < persons.length; i++) {
74             persons[i] = new Person.Builder()
75                     .setBot(false)
76                     .setIcon(icon)
77                     .setName("google" + i)
78                     .setImportant(true)
79                     .build();
80         }
81 
82         ShortcutInfo shortcut = new ShortcutInfo.Builder(context, "BubbleChat")
83                 .setShortLabel("BubbleChat")
84                 .setLongLived(true)
85                 .setIntent(new Intent(Intent.ACTION_VIEW))
86                 .setIcon(Icon.createWithResource(context, R.drawable.ic_message))
87                 .setPersons(persons)
88                 .build();
89         ShortcutManager scmanager = context.getSystemService(ShortcutManager.class);
90         scmanager.addDynamicShortcuts(Arrays.asList(shortcut));
91     }
92 
93 }
94