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 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) 55 .addCategory(Intent.CATEGORY_DEFAULT) 56 .setPackage("com.android.htmlviewer"); 57 try { 58 getContext().startActivity(intent); 59 } catch (ActivityNotFoundException e) { 60 Log.e(TAG, "Failed to find viewer", e); 61 showError(); 62 } 63 } 64 showError()65 private void showError() { 66 Toast.makeText( 67 getContext(), R.string.settings_license_activity_unavailable, Toast.LENGTH_LONG) 68 .show(); 69 } 70 } 71