2012-04-18 04:06:54 -07:00
|
|
|
package me.shkschneider.dropbearserver.Tasks;
|
2012-04-17 08:30:14 -07:00
|
|
|
|
2012-04-23 07:21:45 -07:00
|
|
|
import me.shkschneider.dropbearserver.Utils.ServerUtils;
|
2012-04-22 16:39:18 -07:00
|
|
|
import me.shkschneider.dropbearserver.Utils.ShellUtils;
|
2012-04-17 08:30:14 -07:00
|
|
|
import android.app.ProgressDialog;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.os.AsyncTask;
|
2012-04-23 09:17:13 -07:00
|
|
|
import android.util.Log;
|
2012-04-17 08:30:14 -07:00
|
|
|
|
2012-04-23 01:45:12 -07:00
|
|
|
public class ServerStopper extends AsyncTask<Void, String, Boolean> {
|
2012-04-27 04:41:06 -07:00
|
|
|
|
2012-04-24 13:53:15 -07:00
|
|
|
private static final String TAG = "DropBearServer";
|
2012-04-17 08:30:14 -07:00
|
|
|
|
2012-04-25 08:10:02 -07:00
|
|
|
private Context mContext = null;
|
|
|
|
private ProgressDialog mProgressDialog = null;
|
2012-04-19 02:28:19 -07:00
|
|
|
|
2012-04-18 04:06:54 -07:00
|
|
|
private ServerStopperCallback<Boolean> mCallback;
|
2012-04-17 08:30:14 -07:00
|
|
|
|
2012-06-12 08:14:54 -07:00
|
|
|
public ServerStopper(Context context, ServerStopperCallback<Boolean> callback) {
|
2012-04-17 08:30:14 -07:00
|
|
|
mContext = context;
|
2012-04-19 02:28:19 -07:00
|
|
|
mCallback = callback;
|
|
|
|
if (mContext != null) {
|
|
|
|
mProgressDialog = new ProgressDialog(mContext);
|
|
|
|
mProgressDialog.setTitle("Stopping server");
|
|
|
|
mProgressDialog.setMessage("Please wait...");
|
|
|
|
mProgressDialog.setCancelable(false);
|
|
|
|
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
|
|
|
|
mProgressDialog.setMax(100);
|
|
|
|
mProgressDialog.setIcon(0);
|
|
|
|
}
|
2012-04-17 08:30:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPreExecute() {
|
|
|
|
super.onPreExecute();
|
2012-04-19 02:28:19 -07:00
|
|
|
if (mProgressDialog != null) {
|
|
|
|
mProgressDialog.show();
|
|
|
|
}
|
2012-04-17 08:30:14 -07:00
|
|
|
}
|
2012-04-27 04:41:06 -07:00
|
|
|
|
2012-04-24 14:52:40 -07:00
|
|
|
private Boolean falseWithError(String error) {
|
|
|
|
Log.d(TAG, "ServerStopper: " + error);
|
2012-04-26 09:06:36 -07:00
|
|
|
//Toast.makeText(mContext, "Error: " + error, Toast.LENGTH_LONG).show();
|
2012-04-24 14:52:40 -07:00
|
|
|
return false;
|
|
|
|
}
|
2012-04-27 04:41:06 -07:00
|
|
|
|
2012-04-17 08:30:14 -07:00
|
|
|
@Override
|
|
|
|
protected Boolean doInBackground(Void... params) {
|
2012-04-24 13:53:15 -07:00
|
|
|
Log.i(TAG, "ServerStopper: doInBackground()");
|
2012-04-27 04:41:06 -07:00
|
|
|
|
2012-06-13 01:52:11 -07:00
|
|
|
String pidFile = ServerUtils.getLocalDir(mContext) + "/pid";
|
|
|
|
ShellUtils.rm(pidFile);
|
|
|
|
|
2012-06-12 08:14:54 -07:00
|
|
|
String lockFile = ServerUtils.getLocalDir(mContext) + "/lock";
|
|
|
|
if (ShellUtils.echoToFile("0", lockFile) == false)
|
|
|
|
return falseWithError("echoToFile(0, " + lockFile + ")");
|
2012-04-23 09:17:13 -07:00
|
|
|
|
2012-06-12 08:14:54 -07:00
|
|
|
Log.i(TAG, "ServerStopper: Killing processes");
|
|
|
|
if (ShellUtils.killall("dropbear") == false)
|
|
|
|
return falseWithError("killall(dropbear)");
|
2012-04-27 04:41:06 -07:00
|
|
|
|
2012-04-17 08:30:14 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Boolean result) {
|
2012-04-24 03:55:04 -07:00
|
|
|
if (mProgressDialog != null) {
|
|
|
|
mProgressDialog.dismiss();
|
|
|
|
}
|
2012-04-18 04:06:54 -07:00
|
|
|
if (mCallback != null) {
|
|
|
|
mCallback.onServerStopperComplete(result);
|
2012-04-17 08:30:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|