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