Home | History | Annotate | Download | only in instantapps
      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.settings.applications.instantapps;
     18 
     19 import android.app.AlertDialog;
     20 import android.app.Fragment;
     21 import android.content.Context;
     22 import android.content.DialogInterface;
     23 import android.content.Intent;
     24 import android.content.pm.PackageManager;
     25 import android.os.UserHandle;
     26 import android.view.View;
     27 import android.widget.Button;
     28 
     29 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
     30 import com.android.settings.R;
     31 import com.android.settings.applications.AppStoreUtil;
     32 import com.android.settings.applications.PackageManagerWrapper;
     33 import com.android.settings.applications.PackageManagerWrapperImpl;
     34 import com.android.settings.overlay.FeatureFactory;
     35 
     36 /** Encapsulates a container for buttons relevant to instant apps */
     37 public class InstantAppButtonsController implements DialogInterface.OnClickListener {
     38 
     39     public interface ShowDialogDelegate {
     40         /**
     41          * Delegate that should be called when this controller wants to show a dialog.
     42          */
     43         void showDialog(int id);
     44     }
     45 
     46     private final Context mContext;
     47     private final Fragment mFragment;
     48     private final View mView;
     49     private final PackageManagerWrapper mPackageManagerWrapper;
     50     private final ShowDialogDelegate mShowDialogDelegate;
     51     private String mPackageName;
     52 
     53     public static final int DLG_BASE = 0x5032;
     54     public static final int DLG_CLEAR_APP = DLG_BASE + 1;
     55 
     56     public InstantAppButtonsController(
     57             Context context,
     58             Fragment fragment,
     59             View view,
     60             ShowDialogDelegate showDialogDelegate) {
     61       mContext = context;
     62       mFragment = fragment;
     63       mView = view;
     64       mShowDialogDelegate = showDialogDelegate;
     65       mPackageManagerWrapper = new PackageManagerWrapperImpl(context.getPackageManager());
     66     }
     67 
     68     public InstantAppButtonsController setPackageName(String packageName) {
     69         mPackageName = packageName;
     70         return this;
     71     }
     72 
     73     public void bindButtons() {
     74         Button installButton = (Button)mView.findViewById(R.id.install);
     75         Button clearDataButton = (Button)mView.findViewById(R.id.clear_data);
     76         Intent appStoreIntent = AppStoreUtil.getAppStoreLink(mContext, mPackageName);
     77         if (appStoreIntent != null) {
     78             installButton.setEnabled(true);
     79             installButton.setOnClickListener(v -> mFragment.startActivity(appStoreIntent));
     80         }
     81 
     82         clearDataButton.setOnClickListener(v -> mShowDialogDelegate.showDialog(DLG_CLEAR_APP));
     83     }
     84 
     85     public AlertDialog createDialog(int id) {
     86         if (id == DLG_CLEAR_APP) {
     87             AlertDialog dialog = new AlertDialog.Builder(mFragment.getActivity())
     88                     .setPositiveButton(R.string.clear_instant_app_data, this)
     89                     .setNegativeButton(R.string.cancel, null)
     90                     .setTitle(R.string.clear_instant_app_data)
     91                     .setMessage(mContext.getString(R.string.clear_instant_app_confirmation))
     92                     .create();
     93             return dialog;
     94         }
     95         return null;
     96     }
     97 
     98     public void onClick(DialogInterface dialog, int which) {
     99         if (which == DialogInterface.BUTTON_POSITIVE) {
    100             FeatureFactory.getFactory(mContext)
    101                     .getMetricsFeatureProvider()
    102                     .action(mContext,
    103                             MetricsEvent.ACTION_SETTINGS_CLEAR_INSTANT_APP,
    104                             mPackageName);
    105             mPackageManagerWrapper.deletePackageAsUser(
    106                     mPackageName, null, 0, UserHandle.myUserId());
    107         }
    108     }
    109 
    110     public InstantAppButtonsController show() {
    111         bindButtons();
    112         mView.setVisibility(View.VISIBLE);
    113         return this;
    114     }
    115 }
    116