Alan Viverette | b6a2573 | 2017-11-21 14:49:24 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.server.am; |
| 18 | |
| 19 | import android.app.AlertDialog; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.pm.ApplicationInfo; |
| 23 | import android.content.pm.PackageManager; |
| 24 | import android.view.Window; |
| 25 | import android.view.WindowManager; |
| 26 | import android.widget.CheckBox; |
| 27 | |
| 28 | import com.android.internal.R; |
| 29 | import com.android.server.utils.AppInstallerUtil; |
| 30 | |
| 31 | public class UnsupportedCompileSdkDialog { |
| 32 | private final AlertDialog mDialog; |
| 33 | private final String mPackageName; |
| 34 | |
| 35 | public UnsupportedCompileSdkDialog(final AppWarnings manager, Context context, |
| 36 | ApplicationInfo appInfo) { |
| 37 | mPackageName = appInfo.packageName; |
| 38 | |
| 39 | final PackageManager pm = context.getPackageManager(); |
| 40 | final CharSequence label = appInfo.loadSafeLabel(pm); |
| 41 | final CharSequence message = context.getString(R.string.unsupported_compile_sdk_message, |
Alan Viverette | 9fdd44f | 2017-11-28 13:39:16 -0500 | [diff] [blame] | 42 | label); |
Alan Viverette | b6a2573 | 2017-11-21 14:49:24 -0500 | [diff] [blame] | 43 | |
| 44 | final AlertDialog.Builder builder = new AlertDialog.Builder(context) |
| 45 | .setPositiveButton(R.string.ok, null) |
| 46 | .setMessage(message) |
| 47 | .setView(R.layout.unsupported_compile_sdk_dialog_content); |
| 48 | |
| 49 | // If we might be able to update the app, show a button. |
| 50 | final Intent installerIntent = AppInstallerUtil.createIntent(context, appInfo.packageName); |
| 51 | if (installerIntent != null) { |
| 52 | builder.setNeutralButton(R.string.unsupported_compile_sdk_check_update, |
| 53 | (dialog, which) -> context.startActivity(installerIntent)); |
| 54 | } |
| 55 | |
| 56 | // Ensure the content view is prepared. |
| 57 | mDialog = builder.create(); |
| 58 | mDialog.create(); |
| 59 | |
| 60 | final Window window = mDialog.getWindow(); |
| 61 | window.setType(WindowManager.LayoutParams.TYPE_PHONE); |
| 62 | |
| 63 | // DO NOT MODIFY. Used by CTS to verify the dialog is displayed. |
| 64 | window.getAttributes().setTitle("UnsupportedCompileSdkDialog"); |
| 65 | |
| 66 | final CheckBox alwaysShow = mDialog.findViewById(R.id.ask_checkbox); |
| 67 | alwaysShow.setChecked(true); |
| 68 | alwaysShow.setOnCheckedChangeListener((buttonView, isChecked) -> manager.setPackageFlag( |
| 69 | mPackageName, AppWarnings.FLAG_HIDE_COMPILE_SDK, !isChecked)); |
| 70 | } |
| 71 | |
| 72 | public String getPackageName() { |
| 73 | return mPackageName; |
| 74 | } |
| 75 | |
| 76 | public void show() { |
| 77 | mDialog.show(); |
| 78 | } |
| 79 | |
| 80 | public void dismiss() { |
| 81 | mDialog.dismiss(); |
| 82 | } |
| 83 | } |