Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert AsyncTasks to IntentService #90

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@
<service android:name="com.prey.services.PreyDisablePowerOptionsService" />
<service android:name="com.prey.beta.services.PreyBetaRunnerService" />
<service android:name="com.prey.actions.report.ReportService" />
<service android:name="com.prey.services.AddDeviceToApiKeyBatch" android:exported="false"/>
<service android:name="com.prey.services.DetachDeviceService" android:exported="false"/>
<service android:name="com.prey.services.RevokedPasswordPhraseService" android:exported="false"/>
<service android:name="com.prey.activities.LoadContactService" android:exported="false"/>
<service android:name="com.prey.services.CreateAccountService" android:exported="false"/>
<service android:name="com.prey.services.CheckPasswordService" android:exported="false"/>
<service android:name="com.prey.services.AddDeviceToAccountService" android:exported="false"/>

<!-- Receivers -->
<receiver android:name="com.prey.receivers.SmsReceiver">
Expand Down
60 changes: 27 additions & 33 deletions src/com/prey/activities/AddDeviceToAccountActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
Expand All @@ -32,22 +33,21 @@
import android.widget.TextView;
import android.widget.Toast;

import com.prey.PreyAccountData;
import com.prey.PreyConfig;
import com.prey.PreyLogger;
import com.prey.R;
import com.prey.exceptions.NoMoreDevicesAllowedException;
import com.prey.exceptions.PreyException;
import com.prey.net.PreyWebServices;
import com.prey.services.AddDeviceToAccountService;
import com.prey.util.KeyboardStatusDetector;
import com.prey.util.KeyboardVisibilityListener;

public class AddDeviceToAccountActivity extends SetupActivity {
public static final String ADDDEVICE_FILTER = "AddDeviceToAccountActivity_receiver";

private static final int NO_MORE_DEVICES_WARNING = 0;
private static final int ERROR = 3;
private String error = null;
private boolean noMoreDeviceError = false;
private AddDeviceToAccountReceiver receiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -87,6 +87,8 @@ public void onVisibilityChanged(boolean keyboardVisible) {
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.layout.add_device);
InputMethodManager im = (InputMethodManager) getSystemService(Service.INPUT_METHOD_SERVICE);

receiver = new AddDeviceToAccountReceiver();
registerReceiver(receiver, new IntentFilter(ADDDEVICE_FILTER));
Button ok = (Button) findViewById(R.id.add_device_btn_ok);
ok.setOnClickListener(new View.OnClickListener() {

Expand All @@ -104,7 +106,11 @@ public void onClick(View v) {
if(password.length()<6||password.length()>32){
Toast.makeText(ctx, ctx.getString(R.string.error_password_out_of_range,6,32), Toast.LENGTH_LONG).show();
}else{
new AddDeviceToAccount().execute(email, password, getDeviceType(ctx));
Intent addDevice = new Intent(AddDeviceToAccountActivity.this, AddDeviceToAccountService.class);
receiver.showProgressDialog();
String[] params = { email, password, getDeviceType(ctx) };
addDevice.putExtra("params", params);
AddDeviceToAccountActivity.this.startService(addDevice);
}
}
}
Expand Down Expand Up @@ -146,6 +152,13 @@ public void onClick(View v) {

}

@Override
public void onDestroy() {
super.onDestroy();
if (receiver != null)
unregisterReceiver(receiver);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Expand Down Expand Up @@ -213,12 +226,11 @@ public void onClick(DialogInterface dialog, int id) {
}
}

private class AddDeviceToAccount extends AsyncTask<String, Void, Void> {
private class AddDeviceToAccountReceiver extends BroadcastReceiver {

ProgressDialog progressDialog = null;

@Override
protected void onPreExecute() {
public void showProgressDialog() {
progressDialog = new ProgressDialog(AddDeviceToAccountActivity.this);
progressDialog.setMessage(AddDeviceToAccountActivity.this.getText(R.string.set_old_user_loading).toString());
progressDialog.setIndeterminate(true);
Expand All @@ -227,30 +239,12 @@ protected void onPreExecute() {
}

@Override
protected Void doInBackground(String... data) {
try {
noMoreDeviceError = false;
error = null;
PreyAccountData accountData = PreyWebServices.getInstance().registerNewDeviceToAccount(AddDeviceToAccountActivity.this, data[0], data[1], data[2]);
getPreyConfig().saveAccount(accountData);

} catch (PreyException e) {
error = e.getMessage();
try {
NoMoreDevicesAllowedException noMoreDevices = (NoMoreDevicesAllowedException) e;
noMoreDeviceError = true;

} catch (ClassCastException e1) {
noMoreDeviceError = false;
}
}
return null;
}

@Override
protected void onPostExecute(Void unused) {
public void onReceive(Context receiverContext, Intent receiverIntent) {
error = receiverIntent.getStringExtra("error");
noMoreDeviceError = receiverIntent.getBooleanExtra("noMoreDeviceError", false);
try {
progressDialog.dismiss();
if (progressDialog != null)
progressDialog.dismiss();
} catch (Exception e) {
}
if (noMoreDeviceError)
Expand All @@ -264,7 +258,7 @@ protected void onPostExecute(Void unused) {
Intent intent = new Intent(AddDeviceToAccountActivity.this, PermissionInformationActivity.class);
intent.putExtras(bundle);
startActivity(intent);
finish();
AddDeviceToAccountActivity.this.finish();
} else
showDialog(ERROR);
}
Expand Down
53 changes: 28 additions & 25 deletions src/com/prey/activities/CreateAccountActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@
import java.util.Locale;

import android.app.AlertDialog;

import com.prey.R;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
Expand All @@ -31,19 +30,21 @@
import android.widget.TextView;
import android.widget.Toast;

import com.prey.PreyAccountData;
import com.prey.PreyLogger;
import com.prey.exceptions.PreyException;
import com.prey.net.PreyWebServices;
import com.prey.R;
import com.prey.services.CreateAccountService;
import com.prey.util.KeyboardStatusDetector;
import com.prey.util.KeyboardVisibilityListener;

public class CreateAccountActivity extends SetupActivity {
public static final String CREATEACCOUNT_FILTER = "CreateAccountReceiver_receiver";

private static final int ERROR = 1;
private String password = null;
private String name = null;
private String email = null;
private String error = null;
private CreateAccountReceiver receiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -80,6 +81,8 @@ public void onVisibilityChanged(boolean keyboardVisible) {
}
});

receiver = new CreateAccountReceiver();
this.registerReceiver(receiver, new IntentFilter(CREATEACCOUNT_FILTER));
Button ok = (Button) findViewById(R.id.new_account_btn_ok);
ok.setOnClickListener(new View.OnClickListener() {

Expand All @@ -97,7 +100,11 @@ public void onClick(View v) {
if(password.length()<6||password.length()>32){
Toast.makeText(ctx, ctx.getString(R.string.error_password_out_of_range,6,32), Toast.LENGTH_LONG).show();
}else{
new CreateAccount().execute(name, email, password);
Intent createAccount = new Intent(CreateAccountActivity.this, CreateAccountService.class);
receiver.showProgressDialog();
String[] params = { name, email, password };
createAccount.putExtra("params", params);
CreateAccountActivity.this.startService(createAccount);
}
}
}
Expand Down Expand Up @@ -143,18 +150,24 @@ public void onClick(View v) {

}

@Override
public void onDestroy() {
super.onDestroy();
if (receiver != null)
unregisterReceiver(receiver);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

}

private class CreateAccount extends AsyncTask<String, Void, Void> {
private class CreateAccountReceiver extends BroadcastReceiver {

ProgressDialog progressDialog = null;

@Override
protected void onPreExecute() {
public void showProgressDialog() {
progressDialog = new ProgressDialog(CreateAccountActivity.this);
progressDialog.setMessage(CreateAccountActivity.this.getText(R.string.creating_account_please_wait).toString());
progressDialog.setIndeterminate(true);
Expand All @@ -163,21 +176,11 @@ protected void onPreExecute() {
}

@Override
protected Void doInBackground(String... data) {
public void onReceive(Context receiverContext, Intent receiverIntent) {
error = receiverIntent.getStringExtra("error");
try {
PreyAccountData accountData = PreyWebServices.getInstance().registerNewAccount(CreateAccountActivity.this, data[0], data[1], data[2], getDeviceType());
PreyLogger.d("Response creating account: " + accountData.toString());
getPreyConfig().saveAccount(accountData);
} catch (PreyException e) {
error = e.getMessage();
}
return null;
}

@Override
protected void onPostExecute(Void unused) {
try {
progressDialog.dismiss();
if (progressDialog != null)
progressDialog.dismiss();
} catch (Exception e) {
}
if (error == null) {
Expand All @@ -187,7 +190,7 @@ protected void onPostExecute(Void unused) {
Intent intent = new Intent(CreateAccountActivity.this, PermissionInformationActivity.class);
intent.putExtras(bundle);
startActivity(intent);
finish();
CreateAccountActivity.this.finish();
} else
showDialog(ERROR);
}
Expand Down
Loading