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.AssociationRequest.DEVICE_PROFILE_APP_STREAMING;
20 import static android.companion.AssociationRequest.DEVICE_PROFILE_COMPUTER;
21 import static android.companion.AssociationRequest.DEVICE_PROFILE_NEARBY_DEVICE_STREAMING;
22 
23 import static com.android.companiondevicemanager.Utils.getApplicationIcon;
24 import static com.android.companiondevicemanager.Utils.getApplicationLabel;
25 import static com.android.companiondevicemanager.Utils.getHtmlFromResources;
26 
27 import android.annotation.Nullable;
28 import android.companion.AssociationRequest;
29 import android.companion.virtual.flags.Flags;
30 import android.content.DialogInterface;
31 import android.content.pm.PackageManager;
32 import android.graphics.drawable.Drawable;
33 import android.os.Bundle;
34 import android.text.Spanned;
35 import android.util.Log;
36 import android.view.LayoutInflater;
37 import android.view.View;
38 import android.view.ViewGroup;
39 import android.widget.Button;
40 import android.widget.ImageView;
41 import android.widget.TextView;
42 
43 import androidx.annotation.NonNull;
44 import androidx.fragment.app.DialogFragment;
45 
46 /**
47  * A fragmentDialog shows additional information about selfManaged devices
48  */
49 public class CompanionVendorHelperDialogFragment extends DialogFragment {
50     private static final String TAG = "CDM_CompanionVendorHelperDialogFragment";
51     private static final String ASSOCIATION_REQUEST_EXTRA = "association_request";
52 
53     private CompanionVendorHelperDialogListener mListener;
54     // Only present for selfManaged devices.
55     private TextView mTitle;
56     private TextView mSummary;
57     private ImageView mAppIcon;
58     private Button mButton;
59 
60     interface CompanionVendorHelperDialogListener {
onShowHelperDialogFailed()61         void onShowHelperDialogFailed();
onHelperDialogDismissed()62         void onHelperDialogDismissed();
63     }
64 
CompanionVendorHelperDialogFragment()65     private CompanionVendorHelperDialogFragment() {}
66 
newInstance(AssociationRequest request)67     static CompanionVendorHelperDialogFragment newInstance(AssociationRequest request) {
68         CompanionVendorHelperDialogFragment fragmentDialog =
69                 new CompanionVendorHelperDialogFragment();
70 
71         Bundle bundle = new Bundle();
72         bundle.putParcelable(ASSOCIATION_REQUEST_EXTRA, request);
73         fragmentDialog.setArguments(bundle);
74 
75         return fragmentDialog;
76     }
77 
78     @Override
onCreate(Bundle savedInstanceState)79     public void onCreate(Bundle savedInstanceState) {
80         super.onCreate(savedInstanceState);
81         mListener = (CompanionVendorHelperDialogListener) getActivity();
82         // Hide the title bar in the dialog.
83         setStyle(STYLE_NO_TITLE, /* Theme */0);
84     }
85 
86     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)87     public View onCreateView(LayoutInflater inflater, ViewGroup container,
88             Bundle savedInstanceState) {
89         return inflater.inflate(R.layout.helper_confirmation, container);
90     }
91 
92     @Override
onDismiss(@onNull DialogInterface dialog)93     public void onDismiss(@NonNull DialogInterface dialog) {
94         super.onDismiss(dialog);
95         mListener.onHelperDialogDismissed();
96     }
97 
98     @Override
onViewCreated(View view, @Nullable Bundle savedInstanceState)99     public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
100         super.onViewCreated(view, savedInstanceState);
101 
102         Drawable applicationIcon;
103         AssociationRequest request = getArguments().getParcelable(
104                 ASSOCIATION_REQUEST_EXTRA, AssociationRequest.class);
105 
106         final String deviceProfile = request.getDeviceProfile();
107         final String packageName = request.getPackageName();
108         final CharSequence displayName = request.getDisplayName();
109         final int userId = request.getUserId();
110         final CharSequence appLabel;
111 
112         try {
113             applicationIcon = getApplicationIcon(getContext(), packageName);
114             appLabel = getApplicationLabel(getContext(), packageName, userId);
115         } catch (PackageManager.NameNotFoundException e) {
116             Log.e(TAG, "Package u" + userId + "/" + packageName + " not found.");
117             mListener.onShowHelperDialogFailed();
118             return;
119         }
120 
121         mTitle = view.findViewById(R.id.helper_title);
122         mSummary = view.findViewById(R.id.helper_summary);
123         mAppIcon = view.findViewById(R.id.app_icon);
124         mButton = view.findViewById(R.id.btn_back);
125 
126         final CharSequence title;
127         final Spanned summary;
128 
129         switch (deviceProfile) {
130             case DEVICE_PROFILE_APP_STREAMING:
131                 title = getHtmlFromResources(getContext(), R.string.helper_title_app_streaming);
132                 summary = getHtmlFromResources(
133                         getContext(), Flags.interactiveScreenMirror()
134                                 ? R.string.helper_summary_app_streaming_with_mirroring
135                                 : R.string.helper_summary_app_streaming, title, displayName);
136                 break;
137 
138             case DEVICE_PROFILE_COMPUTER:
139                 title = getHtmlFromResources(getContext(), R.string.helper_title_computer);
140                 summary = getHtmlFromResources(
141                         getContext(), R.string.helper_summary_computer, title, displayName);
142                 break;
143 
144             case DEVICE_PROFILE_NEARBY_DEVICE_STREAMING:
145                 title = appLabel;
146                 summary = getHtmlFromResources(
147                         getContext(), R.string.helper_summary_nearby_device_streaming, title,
148                         displayName);
149                 break;
150 
151             default:
152                 throw new RuntimeException("Unsupported profile " + deviceProfile);
153         }
154 
155         mTitle.setText(title);
156         mSummary.setText(summary);
157         mAppIcon.setImageDrawable(applicationIcon);
158 
159         mButton.setOnClickListener(v -> {
160             dismiss();
161             mListener.onHelperDialogDismissed();
162         });
163     }
164 }
165