• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.googlecode.android_scripting;
18 
19 import android.app.ProgressDialog;
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.content.SharedPreferences;
23 import android.content.DialogInterface.OnCancelListener;
24 import android.os.AsyncTask;
25 import android.preference.PreferenceManager;
26 
27 import com.googlecode.android_scripting.exception.Sl4aException;
28 import com.googlecode.android_scripting.interpreter.InterpreterConstants;
29 import com.googlecode.android_scripting.interpreter.InterpreterDescriptor;
30 import com.googlecode.android_scripting.interpreter.InterpreterUtils;
31 
32 import java.io.File;
33 import java.util.ArrayList;
34 import java.util.List;
35 
36 /**
37  * AsyncTask for uninstalling interpreters.
38  *
39  */
40 public abstract class InterpreterUninstaller extends AsyncTask<Void, Void, Boolean> {
41 
42   protected final InterpreterDescriptor mDescriptor;
43   protected final Context mContext;
44   protected final ProgressDialog mDialog;
45   protected final AsyncTaskListener<Boolean> mListener;
46 
47   protected final String mInterpreterRoot;
48 
InterpreterUninstaller(InterpreterDescriptor descriptor, Context context, AsyncTaskListener<Boolean> listener)49   public InterpreterUninstaller(InterpreterDescriptor descriptor, Context context,
50       AsyncTaskListener<Boolean> listener) throws Sl4aException {
51 
52     super();
53 
54     mDescriptor = descriptor;
55     mContext = context;
56     mListener = listener;
57 
58     String packageName = mDescriptor.getClass().getPackage().getName();
59 
60     if (packageName.length() == 0) {
61       throw new Sl4aException("Interpreter package name is empty.");
62     }
63 
64     mInterpreterRoot = InterpreterConstants.SDCARD_ROOT + packageName;
65 
66     if (mDescriptor == null) {
67       throw new Sl4aException("Interpreter description not provided.");
68     }
69     if (mDescriptor.getName() == null) {
70       throw new Sl4aException("Interpreter not specified.");
71     }
72     if (!isInstalled()) {
73       throw new Sl4aException("Interpreter not installed.");
74     }
75 
76     if (context != null) {
77       mDialog = new ProgressDialog(context);
78     } else {
79       mDialog = null;
80     }
81   }
82 
execute()83   public final void execute() {
84     execute(null, null, null);
85   }
86 
87   @Override
onPreExecute()88   protected void onPreExecute() {
89     if (mDialog != null) {
90       mDialog.setMessage("Uninstalling " + mDescriptor.getNiceName());
91       mDialog.setIndeterminate(true);
92       mDialog.setOnCancelListener(new OnCancelListener() {
93         @Override
94         public void onCancel(DialogInterface dialog) {
95           cancel(true);
96         }
97       });
98       mDialog.show();
99     }
100   }
101 
102   @Override
onPostExecute(Boolean result)103   protected void onPostExecute(Boolean result) {
104     if (mDialog != null && mDialog.isShowing()) {
105       mDialog.dismiss();
106     }
107     if (result) {
108       mListener.onTaskFinished(result, "Uninstallation successful.");
109     } else {
110       mListener.onTaskFinished(result, "Uninstallation failed.");
111     }
112   }
113 
114   @Override
doInBackground(Void... params)115   protected Boolean doInBackground(Void... params) {
116     List<File> directories = new ArrayList<File>();
117 
118     directories.add(new File(mInterpreterRoot));
119 
120     if (mDescriptor.hasInterpreterArchive()) {
121       directories.add(InterpreterUtils.getInterpreterRoot(mContext, mDescriptor.getName()));
122     }
123 
124     for (File directory : directories) {
125       FileUtils.delete(directory);
126     }
127 
128     return cleanup();
129   }
130 
isInstalled()131   protected boolean isInstalled() {
132     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
133     return preferences.getBoolean(InterpreterConstants.INSTALLED_PREFERENCE_KEY, false);
134   }
135 
cleanup()136   protected abstract boolean cleanup();
137 }
138