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.companiondevicemanager; 18 19 import static android.companion.datatransfer.SystemDataTransferRequest.DATA_TYPE_PERMISSION_SYNC; 20 import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS; 21 22 import static com.android.companiondevicemanager.Utils.getHtmlFromResources; 23 24 import static java.util.Objects.requireNonNull; 25 26 import android.annotation.SuppressLint; 27 import android.app.Activity; 28 import android.companion.datatransfer.PermissionSyncRequest; 29 import android.companion.datatransfer.SystemDataTransferRequest; 30 import android.content.Intent; 31 import android.os.Build; 32 import android.os.Bundle; 33 import android.os.ResultReceiver; 34 import android.util.Log; 35 import android.widget.Button; 36 import android.widget.TextView; 37 38 /** 39 * This activity manages the UI of companion device data transfer. 40 */ 41 @SuppressLint("LongLogTag") 42 public class CompanionDeviceDataTransferActivity extends Activity { 43 44 private static final String LOG_TAG = "CDM_CompanionDeviceDataTransferActivity"; 45 46 // Intent data keys from SystemDataTransferProcessor 47 private static final String EXTRA_PERMISSION_SYNC_REQUEST = "permission_sync_request"; 48 private static final String EXTRA_COMPANION_DEVICE_NAME = "companion_device_name"; 49 private static final String EXTRA_SYSTEM_DATA_TRANSFER_RESULT_RECEIVER = 50 "system_data_transfer_result_receiver"; 51 52 // Intent data keys to SystemDataTransferProcessor 53 private static final int RESULT_CODE_SYSTEM_DATA_TRANSFER_ALLOWED = 0; 54 private static final int RESULT_CODE_SYSTEM_DATA_TRANSFER_DISALLOWED = 1; 55 56 private SystemDataTransferRequest mRequest; 57 private CharSequence mCompanionDeviceName; 58 private ResultReceiver mCdmServiceReceiver; 59 60 @Override onCreate(Bundle savedInstanceState)61 public void onCreate(Bundle savedInstanceState) { 62 super.onCreate(savedInstanceState); 63 64 Log.i(LOG_TAG, "Creating UI for data transfer confirmation."); 65 66 getWindow().addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS); 67 68 setContentView(R.layout.data_transfer_confirmation); 69 70 TextView titleView = findViewById(R.id.title); 71 TextView summaryView = findViewById(R.id.summary); 72 Button allowButton = findViewById(R.id.btn_positive); 73 Button disallowButton = findViewById(R.id.btn_negative); 74 75 final Intent intent = getIntent(); 76 mRequest = intent.getParcelableExtra(EXTRA_PERMISSION_SYNC_REQUEST, 77 PermissionSyncRequest.class); 78 mCompanionDeviceName = intent.getCharSequenceExtra(EXTRA_COMPANION_DEVICE_NAME); 79 mCdmServiceReceiver = intent.getParcelableExtra(EXTRA_SYSTEM_DATA_TRANSFER_RESULT_RECEIVER, 80 ResultReceiver.class); 81 82 requireNonNull(mRequest); 83 requireNonNull(mCdmServiceReceiver); 84 85 final String primaryDeviceName = Build.MODEL; 86 87 if (mRequest.getDataType() == DATA_TYPE_PERMISSION_SYNC) { 88 titleView.setText(getHtmlFromResources(this, 89 R.string.permission_sync_confirmation_title, mCompanionDeviceName, 90 primaryDeviceName)); 91 summaryView.setText(getHtmlFromResources(this, R.string.permission_sync_summary, 92 mCompanionDeviceName)); 93 allowButton.setOnClickListener(v -> allow()); 94 disallowButton.setOnClickListener(v -> disallow()); 95 } 96 } 97 allow()98 private void allow() { 99 Log.i(LOG_TAG, "allow()"); 100 101 sendDataToReceiver(RESULT_CODE_SYSTEM_DATA_TRANSFER_ALLOWED); 102 103 setResultAndFinish(RESULT_CODE_SYSTEM_DATA_TRANSFER_ALLOWED); 104 } 105 disallow()106 private void disallow() { 107 Log.i(LOG_TAG, "disallow()"); 108 109 sendDataToReceiver(RESULT_CODE_SYSTEM_DATA_TRANSFER_DISALLOWED); 110 111 setResultAndFinish(RESULT_CODE_SYSTEM_DATA_TRANSFER_DISALLOWED); 112 } 113 sendDataToReceiver(int cdmResultCode)114 private void sendDataToReceiver(int cdmResultCode) { 115 Bundle data = new Bundle(); 116 if (mRequest instanceof PermissionSyncRequest) { 117 data.putParcelable(EXTRA_PERMISSION_SYNC_REQUEST, (PermissionSyncRequest) mRequest); 118 } 119 mCdmServiceReceiver.send(cdmResultCode, data); 120 } 121 setResultAndFinish(int cdmResultCode)122 private void setResultAndFinish(int cdmResultCode) { 123 setResult(cdmResultCode == RESULT_CODE_SYSTEM_DATA_TRANSFER_ALLOWED 124 ? RESULT_OK : RESULT_CANCELED); 125 finish(); 126 } 127 } 128