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.permissioncontroller.incident.wear;
18 
19 import android.app.Activity;
20 import android.net.Uri;
21 import android.os.IncidentManager;
22 import android.view.View;
23 
24 import com.android.permissioncontroller.incident.PendingList;
25 
26 import kotlin.Unit;
27 
28 /**
29  * Wear-specific view handler for the confirmation activity.
30  */
31 public class ConfirmationActivityWearViewHandler {
32 
33     private final Activity mActivity;
34     private final IncidentManager mIncidentManager;
35     private WearConfirmationActivityViewModel mViewModel;
36 
ConfirmationActivityWearViewHandler(Activity activity, IncidentManager incidentManager)37     public ConfirmationActivityWearViewHandler(Activity activity, IncidentManager incidentManager) {
38         mActivity = activity;
39         mIncidentManager = incidentManager;
40     }
41 
42     /**
43      * Creates and returns the view hierarchy that is managed by this view
44      * handler. This must be called before {@link #updateViewModel}.
45      */
createView()46     public View createView() {
47         WearConfirmationActivityViewModelFactory factory =
48                 new WearConfirmationActivityViewModelFactory();
49         mViewModel = factory.create(
50                 WearConfirmationActivityViewModel.class);
51 
52         return WearConfirmationScreenKt.createView(mActivity, mViewModel);
53     }
54 
55     /**
56      * Updates the wear confirmation activity view model to reflect the specified state.
57      */
updateViewModel(boolean isDenyView, String dialogTitle, String message, Uri uri)58     public void updateViewModel(boolean isDenyView, String dialogTitle, String message, Uri uri) {
59         mViewModel.getShowDialogLiveData().setValue(true);
60         mViewModel.getShowDenyReportLiveData().setValue(isDenyView);
61         WearConfirmationActivityViewModel.ContentArgs args =
62                 new WearConfirmationActivityViewModel.ContentArgs(
63                         dialogTitle,
64                         message,
65                         () -> {
66                             mIncidentManager.denyReport(uri);
67                             mActivity.finish();
68                             return Unit.INSTANCE;
69                         },
70                         () -> {
71                             mIncidentManager.approveReport(uri);
72                             PendingList.getInstance().updateState(mActivity, 0);
73                             mActivity.finish();
74                             return Unit.INSTANCE;
75                         },
76                         () -> {
77                             mIncidentManager.denyReport(uri);
78                             PendingList.getInstance().updateState(mActivity, 0);
79                             mActivity.finish();
80                             return Unit.INSTANCE;
81                         }
82                 );
83         mViewModel.getContentArgsLiveData().setValue(args);
84     }
85 }
86