• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * 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  * @author Damon Kohler (damonkohler@gmail.com)
40  * @author Alexey Reznichenko (alexey.reznichenko@gmail.com)
41  */
42 public abstract class InterpreterUninstaller extends AsyncTask<Void, Void, Boolean> {
43 
44   protected final InterpreterDescriptor mDescriptor;
45   protected final Context mContext;
46   protected final ProgressDialog mDialog;
47   protected final AsyncTaskListener<Boolean> mListener;
48 
49   protected final String mInterpreterRoot;
50 
InterpreterUninstaller(InterpreterDescriptor descriptor, Context context, AsyncTaskListener<Boolean> listener)51   public InterpreterUninstaller(InterpreterDescriptor descriptor, Context context,
52       AsyncTaskListener<Boolean> listener) throws Sl4aException {
53 
54     super();
55 
56     mDescriptor = descriptor;
57     mContext = context;
58     mListener = listener;
59 
60     String packageName = mDescriptor.getClass().getPackage().getName();
61 
62     if (packageName.length() == 0) {
63       throw new Sl4aException("Interpreter package name is empty.");
64     }
65 
66     mInterpreterRoot = InterpreterConstants.SDCARD_ROOT + packageName;
67 
68     if (mDescriptor == null) {
69       throw new Sl4aException("Interpreter description not provided.");
70     }
71     if (mDescriptor.getName() == null) {
72       throw new Sl4aException("Interpreter not specified.");
73     }
74     if (!isInstalled()) {
75       throw new Sl4aException("Interpreter not installed.");
76     }
77 
78     if (context != null) {
79       mDialog = new ProgressDialog(context);
80     } else {
81       mDialog = null;
82     }
83   }
84 
execute()85   public final void execute() {
86     execute(null, null, null);
87   }
88 
89   @Override
onPreExecute()90   protected void onPreExecute() {
91     if (mDialog != null) {
92       mDialog.setMessage("Uninstalling " + mDescriptor.getNiceName());
93       mDialog.setIndeterminate(true);
94       mDialog.setOnCancelListener(new OnCancelListener() {
95         @Override
96         public void onCancel(DialogInterface dialog) {
97           cancel(true);
98         }
99       });
100       mDialog.show();
101     }
102   }
103 
104   @Override
onPostExecute(Boolean result)105   protected void onPostExecute(Boolean result) {
106     if (mDialog != null && mDialog.isShowing()) {
107       mDialog.dismiss();
108     }
109     if (result) {
110       mListener.onTaskFinished(result, "Uninstallation successful.");
111     } else {
112       mListener.onTaskFinished(result, "Uninstallation failed.");
113     }
114   }
115 
116   @Override
doInBackground(Void... params)117   protected Boolean doInBackground(Void... params) {
118     List<File> directories = new ArrayList<File>();
119 
120     directories.add(new File(mInterpreterRoot));
121 
122     if (mDescriptor.hasInterpreterArchive()) {
123       directories.add(InterpreterUtils.getInterpreterRoot(mContext, mDescriptor.getName()));
124     }
125 
126     for (File directory : directories) {
127       FileUtils.delete(directory);
128     }
129 
130     return cleanup();
131   }
132 
isInstalled()133   protected boolean isInstalled() {
134     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
135     return preferences.getBoolean(InterpreterConstants.INSTALLED_PREFERENCE_KEY, false);
136   }
137 
cleanup()138   protected abstract boolean cleanup();
139 }
140