1 /*
2  * Copyright (C) 2023 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.cts.notesapp;
18 
19 import android.Manifest;
20 import android.app.Activity;
21 import android.app.StatusBarManager;
22 import android.content.Intent;
23 import android.content.pm.PackageManager;
24 import android.os.Bundle;
25 import android.view.View;
26 import android.widget.Button;
27 import android.widget.TextView;
28 
29 import androidx.core.app.ActivityCompat;
30 
31 /**
32  * A test activity to be used as the Default notes app for CTS Verifier test.
33  */
34 public class NotesAppActivity extends Activity {
35 
36     private static final Intent API_ACTION =
37             new Intent(Intent.ACTION_LAUNCH_CAPTURE_CONTENT_ACTIVITY_FOR_NOTE);
38     private static final int REQUEST_CODE = 42;
39 
40     private StatusBarManager mStatusBarManager;
41     private TextView mStatusMessageTextView;
42 
43     @Override
onCreate(Bundle savedInstanceState)44     protected void onCreate(Bundle savedInstanceState) {
45         super.onCreate(savedInstanceState);
46         setContentView(R.layout.main);
47 
48         mStatusMessageTextView = findViewById(R.id.status_message);
49 
50         // Set up button firing the capture content intent action.
51         Button fireIntentActionButton = findViewById(R.id.fire_intent_action);
52         fireIntentActionButton.setOnClickListener(unused -> {
53             mStatusMessageTextView.setVisibility(View.INVISIBLE);
54             startActivityForResult(API_ACTION, REQUEST_CODE);
55         });
56 
57         // Set up button that calls the can-use API.
58         mStatusBarManager = getSystemService(StatusBarManager.class);
59         Button callCanUseApiButton = findViewById(R.id.call_can_use_api);
60         callCanUseApiButton.setOnClickListener(unused -> {
61             mStatusMessageTextView.setVisibility(View.INVISIBLE);
62 
63             // Check for permission before making the API call.
64             if (ActivityCompat.checkSelfPermission(NotesAppActivity.this,
65                     Manifest.permission.LAUNCH_CAPTURE_CONTENT_ACTIVITY_FOR_NOTE)
66                     != PackageManager.PERMISSION_GRANTED) {
67                 mStatusMessageTextView.setText(R.string.permission_not_available);
68                 mStatusMessageTextView.setVisibility(View.VISIBLE);
69                 return;
70             }
71 
72             // Perform the API call and update UI state.
73             boolean canUseApiResponse =
74                     mStatusBarManager.canLaunchCaptureContentActivityForNote(NotesAppActivity.this);
75             if (canUseApiResponse) {
76                 mStatusMessageTextView.setText(R.string.can_use_api_returned_true);
77             } else {
78                 mStatusMessageTextView.setText(R.string.can_use_api_returned_false);
79             }
80 
81             mStatusMessageTextView.setVisibility(View.VISIBLE);
82         });
83     }
84 
85     @Override
onActivityResult(int requestCode, int resultCode, Intent data)86     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
87         // Return early for unrelated request codes.
88         if (requestCode != REQUEST_CODE) {
89             return;
90         }
91 
92         // Handle API call failures indicated by RESULT_CANCELED result code.
93         if (resultCode == Activity.RESULT_CANCELED) {
94             mStatusMessageTextView.setText(R.string.api_call_failed);
95             mStatusMessageTextView.setVisibility(View.VISIBLE);
96             return;
97         }
98 
99         // Get the response code from API call and update UI in a switch statement.
100         int apiResponseCode =
101                 data.getIntExtra(Intent.EXTRA_CAPTURE_CONTENT_FOR_NOTE_STATUS_CODE, -1);
102         switch (apiResponseCode) {
103             case Intent.CAPTURE_CONTENT_FOR_NOTE_SUCCESS:
104                 if (data.getData() == null) {
105                     // In case there is no screenshot URI returned, set status to API failed.
106                     mStatusMessageTextView.setText(R.string.api_call_failed);
107                 } else {
108                     mStatusMessageTextView.setText(R.string.launch_and_add);
109                 }
110                 break;
111 
112             case Intent.CAPTURE_CONTENT_FOR_NOTE_USER_CANCELED:
113                 mStatusMessageTextView.setText(R.string.launch_and_cancel);
114                 break;
115 
116             case Intent.CAPTURE_CONTENT_FOR_NOTE_WINDOW_MODE_UNSUPPORTED:
117                 mStatusMessageTextView.setText(R.string.launch_window_unsupported);
118                 break;
119 
120             case Intent.CAPTURE_CONTENT_FOR_NOTE_BLOCKED_BY_ADMIN:
121                 mStatusMessageTextView.setText(R.string.launch_blocked_by_admin);
122                 break;
123 
124             default:
125                 mStatusMessageTextView.setText(R.string.api_call_failed);
126         }
127 
128         mStatusMessageTextView.setVisibility(View.VISIBLE);
129     }
130 }
131