• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.settings.deviceinfo.legal;
18 
19 import android.content.ActivityNotFoundException;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.ModuleInfo;
23 import android.util.Log;
24 import android.widget.Toast;
25 
26 import androidx.preference.Preference;
27 
28 import com.android.settings.R;
29 
30 /**
31  * Preference in a list that represents a mainline module that has a licenses file.
32  */
33 public class ModuleLicensePreference extends Preference {
34     private static final String TAG = "ModuleLicensePreference";
35     private final ModuleInfo mModule;
36 
ModuleLicensePreference(Context context, ModuleInfo module)37     public ModuleLicensePreference(Context context, ModuleInfo module) {
38         super(context);
39         mModule = module;
40         setKey(module.getPackageName());
41         setTitle(module.getName());
42     }
43 
44     @Override
onClick()45     protected void onClick() {
46         // Kick off external viewer due to WebView security restrictions (Settings cannot use
47         // WebView because it is UID 1000).
48         Intent intent = new Intent(Intent.ACTION_VIEW)
49                 .setDataAndType(
50                         ModuleLicenseProvider.getUriForPackage(mModule.getPackageName()),
51                         ModuleLicenseProvider.LICENSE_FILE_MIME_TYPE)
52                 .putExtra(Intent.EXTRA_TITLE, mModule.getName())
53                 .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
54                 .addCategory(Intent.CATEGORY_DEFAULT)
55                 .setPackage("com.android.htmlviewer");
56         try {
57             getContext().startActivity(intent);
58         } catch (ActivityNotFoundException e) {
59             Log.e(TAG, "Failed to find viewer", e);
60             showError();
61         }
62     }
63 
showError()64     private void showError() {
65         Toast.makeText(
66                 getContext(), R.string.settings_license_activity_unavailable, Toast.LENGTH_LONG)
67                 .show();
68     }
69 }
70