From da4f1f8c6a0b23c190f90e68672d2142d4088299 Mon Sep 17 00:00:00 2001 From: Yu Lin Date: Sun, 3 May 2015 22:09:01 -0500 Subject: [PATCH 01/14] convert AddDeviceToApiKeyBatch to IntentService --- AndroidManifest.xml | 1 + .../prey/activities/WelcomeBatchActivity.java | 52 +++++++++---------- .../prey/services/AddDeviceToApiKeyBatch.java | 39 ++++++++++++++ 3 files changed, 66 insertions(+), 26 deletions(-) create mode 100644 src/com/prey/services/AddDeviceToApiKeyBatch.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 154f4b57..cd6d6d41 100755 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -230,6 +230,7 @@ + diff --git a/src/com/prey/activities/WelcomeBatchActivity.java b/src/com/prey/activities/WelcomeBatchActivity.java index 7e5f674e..bf2fc165 100644 --- a/src/com/prey/activities/WelcomeBatchActivity.java +++ b/src/com/prey/activities/WelcomeBatchActivity.java @@ -7,21 +7,24 @@ package com.prey.activities; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.content.pm.ActivityInfo; import android.content.res.Configuration; -import android.os.AsyncTask; import android.os.Bundle; - -import com.prey.PreyAccountData; + import com.prey.PreyConfig; import com.prey.PreyScheduled; import com.prey.R; -import com.prey.exceptions.PreyException; -import com.prey.net.PreyWebServices; +import com.prey.services.AddDeviceToApiKeyBatch; public class WelcomeBatchActivity extends PreyActivity { + public static final String KEYBATCHRECEIVER_FILTER = "WelcomeBatchActivity_RECEIVER"; + private String error = null; + private AddDeviceToApiKeyBatchReceiver receiver; @Override public void onConfigurationChanged(Configuration newConfig) { @@ -39,31 +42,28 @@ protected void onCreate(Bundle savedInstanceState) { } + @Override + public void onDestroy() { + super.onDestroy(); + if (receiver != null) { + unregisterReceiver(receiver); + } + } private void installBatch() { - new AddDeviceToApiKeyBatch().execute(getPreyConfig().getApiKeyBatch(),getPreyConfig().getEmailBatch(), getDeviceType()); + receiver = new AddDeviceToApiKeyBatchReceiver(); + this.registerReceiver(receiver, new IntentFilter(KEYBATCHRECEIVER_FILTER)); + Intent addToKeyBatch = new Intent(this, AddDeviceToApiKeyBatch.class); + String[] params = { getPreyConfig().getApiKeyBatch(), + getPreyConfig().getEmailBatch(), getDeviceType() }; + addToKeyBatch.putExtra("params", params); + this.startService(addToKeyBatch); } - private class AddDeviceToApiKeyBatch extends AsyncTask { - @Override - protected void onPreExecute() { - - } - - @Override - protected Void doInBackground(String... data) { - try { - error = null; - PreyAccountData accountData =PreyWebServices.getInstance().registerNewDeviceWithApiKeyEmail(WelcomeBatchActivity.this, data[0], data[1], data[2]); - getPreyConfig().saveAccount(accountData); - } catch (PreyException e) { - error = e.getMessage(); - } - return null; - } - + private class AddDeviceToApiKeyBatchReceiver extends BroadcastReceiver { @Override - protected void onPostExecute(Void unused) { + public void onReceive(Context receiverContext, Intent receiverIntent) { + error = receiverIntent.getStringExtra("error"); if (error == null) { String message = getString(R.string.device_added_congratulations_text); Bundle bundle = new Bundle(); @@ -76,7 +76,7 @@ protected void onPostExecute(Void unused) { if (PreyConfig.getPreyConfig(WelcomeBatchActivity.this).isScheduled()) { PreyScheduled.getInstance(WelcomeBatchActivity.this); } - finish(); + WelcomeBatchActivity.this.finish(); } } } diff --git a/src/com/prey/services/AddDeviceToApiKeyBatch.java b/src/com/prey/services/AddDeviceToApiKeyBatch.java new file mode 100644 index 00000000..7a671e9a --- /dev/null +++ b/src/com/prey/services/AddDeviceToApiKeyBatch.java @@ -0,0 +1,39 @@ +package com.prey.services; + +import android.app.IntentService; +import android.content.Intent; + +import com.prey.PreyAccountData; +import com.prey.PreyConfig; +import com.prey.activities.WelcomeBatchActivity; +import com.prey.exceptions.PreyException; +import com.prey.net.PreyWebServices; + +public class AddDeviceToApiKeyBatch extends IntentService { + private String error; + + public AddDeviceToApiKeyBatch(String name) { + super(name); + } + + public void onHandleIntent(Intent intent) { + String[] data = intent.getStringArrayExtra("params"); + try { + error = null; + PreyAccountData accountData = PreyWebServices.getInstance() + .registerNewDeviceWithApiKeyEmail(this, data[0], data[1], + data[2]); + getPreyConfig().saveAccount(accountData); + } catch (PreyException e) { + error = e.getMessage(); + } + Intent resultIntent = new Intent(WelcomeBatchActivity.KEYBATCHRECEIVER_FILTER); + resultIntent.putExtra("error", error); + sendBroadcast(resultIntent); + return; + } + + private PreyConfig getPreyConfig() { + return PreyConfig.getPreyConfig(this); + } +} From 9efcb3fcb77940f5d7065ce7a1328ff90db9446c Mon Sep 17 00:00:00 2001 From: Yu Lin Date: Sun, 3 May 2015 22:10:57 -0500 Subject: [PATCH 02/14] convert DetachDevice AsyncTask to IntentService --- AndroidManifest.xml | 1 + .../preferences/DetachDevicePreferences.java | 51 ++++++++++--------- .../prey/services/DetachDeviceService.java | 33 ++++++++++++ 3 files changed, 60 insertions(+), 25 deletions(-) create mode 100644 src/com/prey/services/DetachDeviceService.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index cd6d6d41..4821ef63 100755 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -231,6 +231,7 @@ + diff --git a/src/com/prey/preferences/DetachDevicePreferences.java b/src/com/prey/preferences/DetachDevicePreferences.java index 599b975f..b9f662f9 100644 --- a/src/com/prey/preferences/DetachDevicePreferences.java +++ b/src/com/prey/preferences/DetachDevicePreferences.java @@ -7,22 +7,24 @@ package com.prey.preferences; import android.app.ProgressDialog; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; -import android.os.AsyncTask; +import android.content.IntentFilter; import android.os.Bundle; import android.preference.DialogPreference; import android.util.AttributeSet; import android.widget.Toast; -import com.prey.PreyConfig; -import com.prey.activities.LoginActivity; -import com.prey.exceptions.PreyException; -import com.prey.net.PreyWebServices; import com.prey.R; +import com.prey.activities.LoginActivity; +import com.prey.services.DetachDeviceService; public class DetachDevicePreferences extends DialogPreference { + public static final String DETACHDEVICE_FILTER = "DetachDevicePreferences_RECEIVER"; + Context ctx = null; + private DetachDeviceReceiver receiver; public DetachDevicePreferences(Context context, AttributeSet attrs) { super(context, attrs); @@ -38,17 +40,28 @@ public DetachDevicePreferences(Context context, AttributeSet attrs, int defStyle public void onClick(DialogInterface dialog, int which) { super.onClick(dialog, which); if (which == DialogInterface.BUTTON_POSITIVE) { - new DetachDevice().execute(); + receiver = new DetachDeviceReceiver(); + ctx.registerReceiver(receiver, new IntentFilter(DETACHDEVICE_FILTER)); + Intent detachDevice = new Intent(ctx, DetachDeviceService.class); + receiver.showProgressDialog(); + ctx.startService(detachDevice); + } + } + + @Override + public void onActivityDestroy() { + super.onActivityDestroy(); + if (receiver != null) { + ctx.unregisterReceiver(receiver); } } - public class DetachDevice extends AsyncTask { + public class DetachDeviceReceiver extends BroadcastReceiver { private String error = null; ProgressDialog progressDialog = null; - @Override - protected void onPreExecute() { + public void showProgressDialog() { progressDialog = new ProgressDialog(getContext()); progressDialog.setMessage(getContext().getText(R.string.preferences_detach_dettaching_message).toString()); progressDialog.setIndeterminate(true); @@ -57,23 +70,11 @@ protected void onPreExecute() { } @Override - protected Void doInBackground(Void... unused) { - try { - PreyConfig.getPreyConfig(getContext()).unregisterC2dm(false); - PreyConfig.getPreyConfig(getContext()).setSecurityPrivilegesAlreadyPrompted(false); - PreyWebServices.getInstance().deleteDevice(ctx); - PreyConfig.getPreyConfig(getContext()).wipeData(); - - } catch (PreyException e) { - e.printStackTrace(); - error = e.getMessage(); + public void onReceive(Context receiverContext, Intent receiverIntent) { + error = receiverIntent.getStringExtra("error"); + if (progressDialog != null) { + progressDialog.dismiss(); } - return null; - } - - @Override - protected void onPostExecute(Void unused) { - progressDialog.dismiss(); if (error != null) { Toast.makeText(getContext(), error, Toast.LENGTH_LONG).show(); showDialog(new Bundle()); diff --git a/src/com/prey/services/DetachDeviceService.java b/src/com/prey/services/DetachDeviceService.java new file mode 100644 index 00000000..f4a74837 --- /dev/null +++ b/src/com/prey/services/DetachDeviceService.java @@ -0,0 +1,33 @@ +package com.prey.services; + +import android.app.IntentService; +import android.content.Intent; + +import com.prey.PreyConfig; +import com.prey.exceptions.PreyException; +import com.prey.net.PreyWebServices; +import com.prey.preferences.DetachDevicePreferences; + +public class DetachDeviceService extends IntentService { + private String error; + + public DetachDeviceService(String name) { + super(name); + } + + public void onHandleIntent(Intent intent) { + try { + PreyConfig.getPreyConfig(this).unregisterC2dm(false); + PreyConfig.getPreyConfig(this).setSecurityPrivilegesAlreadyPrompted(false); + PreyWebServices.getInstance().deleteDevice(this); + PreyConfig.getPreyConfig(this).wipeData(); + } catch (PreyException e) { + e.printStackTrace(); + error = e.getMessage(); + } + Intent resultIntent = new Intent(DetachDevicePreferences.DETACHDEVICE_FILTER); + resultIntent.putExtra("error", error); + sendBroadcast(resultIntent); + return; + } +} From e40a22701ad7908474c612d8c18f2d6d3508158f Mon Sep 17 00:00:00 2001 From: Yu Lin Date: Sun, 3 May 2015 22:12:47 -0500 Subject: [PATCH 03/14] convert RevokedPasswordPhraseTask AsyncTask to IntentService --- AndroidManifest.xml | 1 + .../RevokedPasswordPreferences.java | 56 +++++++++---------- .../RevokedPasswordPhraseService.java | 30 ++++++++++ 3 files changed, 59 insertions(+), 28 deletions(-) create mode 100644 src/com/prey/services/RevokedPasswordPhraseService.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 4821ef63..2eb229a5 100755 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -232,6 +232,7 @@ + diff --git a/src/com/prey/preferences/RevokedPasswordPreferences.java b/src/com/prey/preferences/RevokedPasswordPreferences.java index 32cb8839..49f1de58 100644 --- a/src/com/prey/preferences/RevokedPasswordPreferences.java +++ b/src/com/prey/preferences/RevokedPasswordPreferences.java @@ -8,20 +8,25 @@ import android.app.ProgressDialog; +import android.content.BroadcastReceiver; import android.content.Context; -import android.os.AsyncTask; +import android.content.Intent; +import android.content.IntentFilter; import android.preference.EditTextPreference; import android.util.AttributeSet; import com.prey.PreyConfig; import com.prey.PreyLogger; import com.prey.R; +import com.prey.services.RevokedPasswordPhraseService; public class RevokedPasswordPreferences extends EditTextPreference { - + public static final String REVOKEDPWD_FILTER = "RevokedPasswordPreferences_receiver"; + Context ctx = null; private String error = null; + private RevokedPasswordPhraseReceiver receiver; public RevokedPasswordPreferences(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); @@ -45,25 +50,31 @@ protected void onDialogClosed(boolean positiveResult) { PreyConfig preyConfig = PreyConfig.getPreyConfig(ctx); if (positiveResult){ PreyLogger.d("Activation phrase changed to:" + getText()); - new RevokedPasswordPhraseTask(ctx).execute(getText()); + receiver = new RevokedPasswordPhraseReceiver(); + ctx.registerReceiver(receiver, new IntentFilter(REVOKEDPWD_FILTER)); + receiver.showProgressDialog(); + Intent revokedPwdPhrase = new Intent(ctx, RevokedPasswordPhraseService.class); + revokedPwdPhrase.putExtra("param", getText()); + ctx.startService(revokedPwdPhrase); } else{ preyConfig.setRevokedPassword(false, ""); } } - - - private class RevokedPasswordPhraseTask extends AsyncTask { - ProgressDialog progressDialog = null; - private Context context = null; - - public RevokedPasswordPhraseTask(Context context){ - this.context = context; + @Override + public void onActivityDestroy() { + super.onActivityDestroy(); + if (receiver != null) { + ctx.unregisterReceiver(receiver); } + } + + private class RevokedPasswordPhraseReceiver extends BroadcastReceiver { + + ProgressDialog progressDialog = null; - @Override - protected void onPreExecute() { + public void showProgressDialog() { progressDialog = new ProgressDialog(getContext()); progressDialog.setMessage(getContext().getText(R.string.preferences_admin_device_setting_uninstallation_password).toString()); @@ -73,22 +84,11 @@ protected void onPreExecute() { } @Override - protected Void doInBackground(String... data) { - try { - PreyConfig preyConfig = PreyConfig.getPreyConfig(context); - PreyLogger.d("password [" + getText()+"]"); - preyConfig.setRevokedPassword(true, getText()); - - //PreyWebServices.getInstance().updateActivationPhrase(getContext(), getText()); - } catch (Exception e) { - error = e.getMessage(); + public void onReceive(Context receiverContext, Intent receiverIntent) { + error = receiverIntent.getStringExtra("error"); + if (progressDialog != null) { + progressDialog.dismiss(); } - return null; - } - - @Override - protected void onPostExecute(Void unused) { - progressDialog.dismiss(); } } diff --git a/src/com/prey/services/RevokedPasswordPhraseService.java b/src/com/prey/services/RevokedPasswordPhraseService.java new file mode 100644 index 00000000..8ae8ee62 --- /dev/null +++ b/src/com/prey/services/RevokedPasswordPhraseService.java @@ -0,0 +1,30 @@ +package com.prey.services; + +import android.app.IntentService; +import android.content.Intent; + +import com.prey.PreyConfig; +import com.prey.PreyLogger; +import com.prey.preferences.RevokedPasswordPreferences; + +public class RevokedPasswordPhraseService extends IntentService { + String error; + + public RevokedPasswordPhraseService(String name) { + super(name); + } + + public void onHandleIntent(Intent intent) { + String data = intent.getStringExtra("param"); + try { + PreyConfig preyConfig = PreyConfig.getPreyConfig(this); + PreyLogger.d("password [" + data + "]"); + preyConfig.setRevokedPassword(true, data); + } catch (Exception e) { + error = e.getMessage(); + } + Intent resultIntent = new Intent(RevokedPasswordPreferences.REVOKEDPWD_FILTER); + resultIntent.putExtra("error", error); + sendBroadcast(resultIntent); + } +} From 505cc2699c6afca87970c0654f3f30192fc1d56d Mon Sep 17 00:00:00 2001 From: Yu Lin Date: Sun, 3 May 2015 22:14:27 -0500 Subject: [PATCH 04/14] convert AsyncTask in SMSContactActivity to IntentService --- AndroidManifest.xml | 1 + .../prey/activities/SMSContactActivity.java | 58 ++++++++++++++----- src/com/prey/contacts/ContactAccessor.java | 2 +- src/com/prey/contacts/ContactInfo.java | 2 +- 4 files changed, 45 insertions(+), 18 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 2eb229a5..2040b853 100755 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -233,6 +233,7 @@ + diff --git a/src/com/prey/activities/SMSContactActivity.java b/src/com/prey/activities/SMSContactActivity.java index d3d8a4c0..5862c4c7 100644 --- a/src/com/prey/activities/SMSContactActivity.java +++ b/src/com/prey/activities/SMSContactActivity.java @@ -7,17 +7,19 @@ package com.prey.activities; import android.app.AlertDialog; +import android.app.IntentService; +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.graphics.Bitmap; import android.net.Uri; -import android.os.AsyncTask; import android.os.Bundle; import android.telephony.PhoneNumberUtils; import android.view.View; import android.widget.Button; -import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; @@ -31,8 +33,10 @@ import com.prey.R; public class SMSContactActivity extends PreyActivity { + public static final String LOADCONTACT_FILTER = "SMSContactActivity_receiver"; private static final int PICK_CONTACT_REQUEST = 0; ContactAccessor contactAccesor = new ContactAccessor(); + private BroadcastReceiver receiver; @Override protected void onCreate(Bundle savedInstanceState) { @@ -60,6 +64,22 @@ public void onClick(View v) { } }); + receiver = new BroadcastReceiver() { + @Override + public void onReceive(Context receiverContext, Intent receiverIntent) { + ContactInfo result = (ContactInfo) receiverIntent.getSerializableExtra("contact"); + bindView(result); + showContactNowAlert(); + } + }; + registerReceiver(receiver, new IntentFilter(LOADCONTACT_FILTER)); + } + + @Override + public void onDestroy() { + super.onDestroy(); + if (receiver != null) + unregisterReceiver(receiver); } @Override @@ -126,20 +146,11 @@ private void loadContactInfo(Uri contactUri) { * up the UI thread while waiting for the query to come back, we might * get an "Application Not Responding" dialog. */ - AsyncTask task = new AsyncTask() { - - @Override - protected ContactInfo doInBackground(Uri... uris) { - return contactAccesor.loadContact(getContentResolver(), uris[0]); - } - - @Override - protected void onPostExecute(ContactInfo result) { - bindView(result); - showContactNowAlert(); - } - }; - task.execute(contactUri); + Intent loadContact = new Intent(this, LoadContactService.class); + loadContact.putExtra("contactAccesor", contactAccesor); + Uri[] uris = { contactUri }; + loadContact.putExtra("uris", uris); + this.startService(loadContact); } protected void bindView(ContactInfo contactInfo) { @@ -174,3 +185,18 @@ private void fillScreenInfo(String name, String number, Bitmap photo){ } } + +class LoadContactService extends IntentService { + public LoadContactService(String name) { + super(name); + } + + public void onHandleIntent(Intent intent) { + ContactAccessor contactAccesor = (ContactAccessor) intent.getSerializableExtra("contactAccesor"); + Uri[] uris = (Uri[]) intent.getParcelableArrayExtra("uris"); + Intent resultIntent = new Intent(SMSContactActivity.LOADCONTACT_FILTER); + resultIntent.putExtra("contact", contactAccesor.loadContact(getContentResolver(), uris[0])); + sendBroadcast(resultIntent); + return; + } +} diff --git a/src/com/prey/contacts/ContactAccessor.java b/src/com/prey/contacts/ContactAccessor.java index eea91de3..90a30ec3 100644 --- a/src/com/prey/contacts/ContactAccessor.java +++ b/src/com/prey/contacts/ContactAccessor.java @@ -36,7 +36,7 @@ * social status updates (see {@link android.provider.ContactsContract.StatusUpdates}). * */ -public class ContactAccessor { +public class ContactAccessor implements java.io.Serializable { public Intent getPickContactIntent() { return new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI); diff --git a/src/com/prey/contacts/ContactInfo.java b/src/com/prey/contacts/ContactInfo.java index 24d95222..f81add44 100644 --- a/src/com/prey/contacts/ContactInfo.java +++ b/src/com/prey/contacts/ContactInfo.java @@ -11,7 +11,7 @@ /** * A model object containing contact data. */ -public class ContactInfo { +public class ContactInfo implements java.io.Serializable { private String mDisplayName; private String mPhoneNumber; From a32b0a1385db4f54bf42001c5ea46bfc4555d18e Mon Sep 17 00:00:00 2001 From: Yu Lin Date: Sun, 3 May 2015 22:16:24 -0500 Subject: [PATCH 05/14] convert CreateAccount AsyncTask to IntentService --- AndroidManifest.xml | 1 + .../activities/CreateAccountActivity.java | 53 ++++++++++--------- .../prey/services/CreateAccountService.java | 44 +++++++++++++++ 3 files changed, 73 insertions(+), 25 deletions(-) create mode 100644 src/com/prey/services/CreateAccountService.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 2040b853..d4c55d9b 100755 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -234,6 +234,7 @@ + diff --git a/src/com/prey/activities/CreateAccountActivity.java b/src/com/prey/activities/CreateAccountActivity.java index 7e1f4ba1..ea1630bf 100644 --- a/src/com/prey/activities/CreateAccountActivity.java +++ b/src/com/prey/activities/CreateAccountActivity.java @@ -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; @@ -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) { @@ -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() { @@ -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); } } } @@ -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 { + 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); @@ -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) { @@ -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); } diff --git a/src/com/prey/services/CreateAccountService.java b/src/com/prey/services/CreateAccountService.java new file mode 100644 index 00000000..dab54e75 --- /dev/null +++ b/src/com/prey/services/CreateAccountService.java @@ -0,0 +1,44 @@ +package com.prey.services; + +import android.app.IntentService; +import android.content.Intent; + +import com.prey.PreyAccountData; +import com.prey.PreyConfig; +import com.prey.PreyLogger; +import com.prey.PreyUtils; +import com.prey.activities.CreateAccountActivity; +import com.prey.exceptions.PreyException; +import com.prey.net.PreyWebServices; + +public class CreateAccountService extends IntentService { + String error; + + public CreateAccountService(String name) { + super(name); + } + + public void onHandleIntent(Intent intent) { + String[] data = intent.getStringArrayExtra("params"); + try { + PreyAccountData accountData = PreyWebServices.getInstance() + .registerNewAccount(this, data[0], data[1], data[2], getDeviceType()); + PreyLogger.d("Response creating account: " + accountData.toString()); + getPreyConfig().saveAccount(accountData); + } catch (PreyException e) { + error = e.getMessage(); + } + Intent resultIntent = new Intent(CreateAccountActivity.CREATEACCOUNT_FILTER); + resultIntent.putExtra("error", error); + sendBroadcast(resultIntent); + return; + } + + private String getDeviceType() { + return PreyUtils.getDeviceType(this); + } + + private PreyConfig getPreyConfig() { + return PreyConfig.getPreyConfig(this); + } +} From cb1c8f0f63aea6be903d7b9d0152f0655abd2ad8 Mon Sep 17 00:00:00 2001 From: Yu Lin Date: Sun, 3 May 2015 22:18:18 -0500 Subject: [PATCH 06/14] convert CheckPassword AsyncTask to IntentService --- AndroidManifest.xml | 1 + src/com/prey/activities/PasswordActivity.java | 66 ++++++++++--------- .../prey/services/CheckPasswordService.java | 37 +++++++++++ 3 files changed, 73 insertions(+), 31 deletions(-) create mode 100644 src/com/prey/services/CheckPasswordService.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index d4c55d9b..8d335241 100755 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -235,6 +235,7 @@ + diff --git a/src/com/prey/activities/PasswordActivity.java b/src/com/prey/activities/PasswordActivity.java index 8e59600e..5f983187 100644 --- a/src/com/prey/activities/PasswordActivity.java +++ b/src/com/prey/activities/PasswordActivity.java @@ -7,27 +7,41 @@ package com.prey.activities; import android.app.ProgressDialog; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.graphics.Typeface; -import android.net.Uri; -import android.os.AsyncTask; import android.text.method.PasswordTransformationMethod; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; -import com.prey.events.Event; -import com.prey.events.manager.EventManagerRunner; -import com.prey.exceptions.PreyException; -import com.prey.net.PreyWebServices; -import com.prey.PreyConfig; import com.prey.PreyStatus; import com.prey.R; +import com.prey.events.Event; +import com.prey.events.manager.EventManagerRunner; +import com.prey.services.CheckPasswordService; public class PasswordActivity extends PreyActivity { - + + public static final String CHECKPWD_FILTER = "PasswordActivity_receiver"; int wrongPasswordIntents = 0; + private CheckPasswordReceiver receiver; + + @Override + protected void onCreate(android.os.Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + receiver = new CheckPasswordReceiver(); + registerReceiver(receiver, new IntentFilter(CHECKPWD_FILTER)); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + if (receiver != null) + unregisterReceiver(receiver); + } protected void bindPasswordControls() { Button checkPasswordOkButton = (Button) findViewById(R.id.password_btn_login); @@ -43,7 +57,11 @@ public void onClick(View v) { if(passwordtyped.length()<6||passwordtyped.length()>32){ Toast.makeText(ctx, ctx.getString(R.string.error_password_out_of_range,6,32), Toast.LENGTH_LONG).show(); }else{ - new CheckPassword().execute(passwordtyped); + Intent checkPwd = new Intent(PasswordActivity.this, CheckPasswordService.class); + receiver.showProgressDialog(); + String[] params = { passwordtyped }; + checkPwd.putExtra("params", params); + PasswordActivity.this.startService(checkPwd); } } @@ -92,16 +110,14 @@ public void onClick(View v) { ((TextView) findViewById(R.id.login_h2_text)).setText(h2);*/ } - protected class CheckPassword extends AsyncTask { + protected class CheckPasswordReceiver extends BroadcastReceiver { ProgressDialog progressDialog = null; boolean isPasswordOk = false; - boolean keepAsking = true; String error = null; - @Override - protected void onPreExecute() { + public void showProgressDialog() { try{ progressDialog = new ProgressDialog(PasswordActivity.this); progressDialog.setMessage(PasswordActivity.this.getText(R.string.password_checking_dialog).toString()); @@ -114,23 +130,11 @@ protected void onPreExecute() { } @Override - protected Void doInBackground(String... password) { - try { - String email = getPreyConfig().getEmail(); - isPasswordOk = PreyWebServices.getInstance().checkPassword(PasswordActivity.this, email, password[0]); - //if (isPasswordOk) - //PreyConfig.getPreyConfig(CheckPasswordActivity.this).setPassword(password[0]); - - } catch (PreyException e) { - error = e.getMessage(); - } - return null; - } - - @Override - protected void onPostExecute(Void unused) { + public void onReceive(Context receiverContext, Intent receiverIntent) { + error = receiverIntent.getStringExtra("error"); + isPasswordOk = receiverIntent.getBooleanExtra("isPasswordOk", false); try{ - if (progressDialog.isShowing()){ + if (progressDialog != null && progressDialog.isShowing()){ progressDialog.dismiss(); } }catch(Exception e){ @@ -145,8 +149,8 @@ else if (!isPasswordOk) { wrongPasswordIntents++; if (wrongPasswordIntents == 3) { Toast.makeText(PasswordActivity.this, R.string.password_intents_exceed, Toast.LENGTH_LONG).show(); - setResult(RESULT_CANCELED); - finish(); + PasswordActivity.this.setResult(RESULT_CANCELED); + PasswordActivity.this.finish(); } else { Toast.makeText(PasswordActivity.this, R.string.password_wrong, Toast.LENGTH_SHORT).show(); } diff --git a/src/com/prey/services/CheckPasswordService.java b/src/com/prey/services/CheckPasswordService.java new file mode 100644 index 00000000..6dc57ecd --- /dev/null +++ b/src/com/prey/services/CheckPasswordService.java @@ -0,0 +1,37 @@ +package com.prey.services; + +import android.app.IntentService; +import android.content.Intent; + +import com.prey.PreyConfig; +import com.prey.activities.PasswordActivity; +import com.prey.exceptions.PreyException; +import com.prey.net.PreyWebServices; + +public class CheckPasswordService extends IntentService { + boolean isPasswordOk; + String error; + + public CheckPasswordService(String name) { + super(name); + } + + public void onHandleIntent(Intent intent) { + String[] password = intent.getStringArrayExtra("params"); + try { + String email = getPreyConfig().getEmail(); + isPasswordOk = PreyWebServices.getInstance().checkPassword(this, email, password[0]); + } catch (PreyException e) { + error = e.getMessage(); + } + Intent resultIntent = new Intent(PasswordActivity.CHECKPWD_FILTER); + resultIntent.putExtra("error", error); + resultIntent.putExtra("isPasswordOk", isPasswordOk); + sendBroadcast(resultIntent); + return; + } + + private PreyConfig getPreyConfig() { + return PreyConfig.getPreyConfig(this); + } +} From 72f6f69175a49f9b87f6dd69d0267e9ab8823565 Mon Sep 17 00:00:00 2001 From: Yu Lin Date: Sun, 3 May 2015 22:19:49 -0500 Subject: [PATCH 07/14] convert AddDeviceToAccount AsyncTask to IntentService --- AndroidManifest.xml | 1 + .../AddDeviceToAccountActivity.java | 60 +++++++++---------- .../services/AddDeviceToAccountService.java | 48 +++++++++++++++ 3 files changed, 76 insertions(+), 33 deletions(-) create mode 100644 src/com/prey/services/AddDeviceToAccountService.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 8d335241..5d5ee5cd 100755 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -236,6 +236,7 @@ + diff --git a/src/com/prey/activities/AddDeviceToAccountActivity.java b/src/com/prey/activities/AddDeviceToAccountActivity.java index 087a0c11..f5563ec9 100644 --- a/src/com/prey/activities/AddDeviceToAccountActivity.java +++ b/src/com/prey/activities/AddDeviceToAccountActivity.java @@ -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; @@ -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) { @@ -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() { @@ -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); } } } @@ -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); @@ -213,12 +226,11 @@ public void onClick(DialogInterface dialog, int id) { } } - private class AddDeviceToAccount extends AsyncTask { + 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); @@ -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) @@ -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); } diff --git a/src/com/prey/services/AddDeviceToAccountService.java b/src/com/prey/services/AddDeviceToAccountService.java new file mode 100644 index 00000000..51e57675 --- /dev/null +++ b/src/com/prey/services/AddDeviceToAccountService.java @@ -0,0 +1,48 @@ +package com.prey.services; + +import android.app.IntentService; +import android.content.Intent; + +import com.prey.PreyAccountData; +import com.prey.PreyConfig; +import com.prey.activities.AddDeviceToAccountActivity; +import com.prey.exceptions.NoMoreDevicesAllowedException; +import com.prey.exceptions.PreyException; +import com.prey.net.PreyWebServices; + +public class AddDeviceToAccountService extends IntentService { + String error; + boolean noMoreDeviceError; + + public AddDeviceToAccountService(String name) { + super(name); + } + + public void onHandleIntent(Intent intent) { + String[] data = intent.getStringArrayExtra("params"); + try { + noMoreDeviceError = false; + error = null; + PreyAccountData accountData = PreyWebServices.getInstance() + .registerNewDeviceToAccount(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; + } + } + Intent resultIntent = new Intent(AddDeviceToAccountActivity.ADDDEVICE_FILTER); + resultIntent.putExtra("error", error); + resultIntent.putExtra("noMoreDeviceError", noMoreDeviceError); + sendBroadcast(resultIntent); + return; + } + + private PreyConfig getPreyConfig() { + return PreyConfig.getPreyConfig(this); + } +} From e82b5f8f91bd51c7721c11d8844a7f20fc5aebf8 Mon Sep 17 00:00:00 2001 From: yulin2 Date: Mon, 4 May 2015 20:49:35 -0500 Subject: [PATCH 08/14] rename AddDeviceToAccountService constructor constructor should be empty constructor --- src/com/prey/services/AddDeviceToAccountService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/prey/services/AddDeviceToAccountService.java b/src/com/prey/services/AddDeviceToAccountService.java index 51e57675..82efffdf 100644 --- a/src/com/prey/services/AddDeviceToAccountService.java +++ b/src/com/prey/services/AddDeviceToAccountService.java @@ -14,8 +14,8 @@ public class AddDeviceToAccountService extends IntentService { String error; boolean noMoreDeviceError; - public AddDeviceToAccountService(String name) { - super(name); + public AddDeviceToAccountService() { + super("AddDeviceToAccountService"); } public void onHandleIntent(Intent intent) { From ed73872a201f2d0a703dc9b314180579e6885c6a Mon Sep 17 00:00:00 2001 From: yulin2 Date: Mon, 4 May 2015 20:52:25 -0500 Subject: [PATCH 09/14] rename DetachDeviceService constructor constructor should be empty constructor --- src/com/prey/services/DetachDeviceService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/prey/services/DetachDeviceService.java b/src/com/prey/services/DetachDeviceService.java index f4a74837..b71d00a8 100644 --- a/src/com/prey/services/DetachDeviceService.java +++ b/src/com/prey/services/DetachDeviceService.java @@ -11,8 +11,8 @@ public class DetachDeviceService extends IntentService { private String error; - public DetachDeviceService(String name) { - super(name); + public DetachDeviceService() { + super("DetachDeviceService"); } public void onHandleIntent(Intent intent) { From d0f7ecc06b611907a0cab0d7b22deb6dcee22be0 Mon Sep 17 00:00:00 2001 From: yulin2 Date: Mon, 4 May 2015 20:53:23 -0500 Subject: [PATCH 10/14] rename RevokedPasswordPhraseService constructor --- src/com/prey/services/RevokedPasswordPhraseService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/prey/services/RevokedPasswordPhraseService.java b/src/com/prey/services/RevokedPasswordPhraseService.java index 8ae8ee62..3cbe9dc1 100644 --- a/src/com/prey/services/RevokedPasswordPhraseService.java +++ b/src/com/prey/services/RevokedPasswordPhraseService.java @@ -10,8 +10,8 @@ public class RevokedPasswordPhraseService extends IntentService { String error; - public RevokedPasswordPhraseService(String name) { - super(name); + public RevokedPasswordPhraseService() { + super("RevokedPasswordPhraseService"); } public void onHandleIntent(Intent intent) { From b2566c404b54861e0caf6c44fb41fac1b4a65615 Mon Sep 17 00:00:00 2001 From: yulin2 Date: Mon, 4 May 2015 20:54:35 -0500 Subject: [PATCH 11/14] rename LoadContactService constructor --- src/com/prey/activities/SMSContactActivity.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/prey/activities/SMSContactActivity.java b/src/com/prey/activities/SMSContactActivity.java index 5862c4c7..673f692d 100644 --- a/src/com/prey/activities/SMSContactActivity.java +++ b/src/com/prey/activities/SMSContactActivity.java @@ -187,8 +187,8 @@ private void fillScreenInfo(String name, String number, Bitmap photo){ } class LoadContactService extends IntentService { - public LoadContactService(String name) { - super(name); + public LoadContactService() { + super("LoadContactService"); } public void onHandleIntent(Intent intent) { From a971692c9e851dbc3bc63d87ffa717840a236ec2 Mon Sep 17 00:00:00 2001 From: yulin2 Date: Mon, 4 May 2015 20:55:34 -0500 Subject: [PATCH 12/14] rename CreateAccountService constructor --- src/com/prey/services/CreateAccountService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/prey/services/CreateAccountService.java b/src/com/prey/services/CreateAccountService.java index dab54e75..4e76fa6f 100644 --- a/src/com/prey/services/CreateAccountService.java +++ b/src/com/prey/services/CreateAccountService.java @@ -14,8 +14,8 @@ public class CreateAccountService extends IntentService { String error; - public CreateAccountService(String name) { - super(name); + public CreateAccountService() { + super("CreateAccountService"); } public void onHandleIntent(Intent intent) { From 2f28027c7273f5da200a15b917d7e730ef5f8f72 Mon Sep 17 00:00:00 2001 From: yulin2 Date: Mon, 4 May 2015 20:56:26 -0500 Subject: [PATCH 13/14] rename CheckPasswordService constructor --- src/com/prey/services/CheckPasswordService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/prey/services/CheckPasswordService.java b/src/com/prey/services/CheckPasswordService.java index 6dc57ecd..1154eb95 100644 --- a/src/com/prey/services/CheckPasswordService.java +++ b/src/com/prey/services/CheckPasswordService.java @@ -12,8 +12,8 @@ public class CheckPasswordService extends IntentService { boolean isPasswordOk; String error; - public CheckPasswordService(String name) { - super(name); + public CheckPasswordService() { + super("CheckPasswordService"); } public void onHandleIntent(Intent intent) { From 40cf98a774a26f12523385410e09626767a76bcd Mon Sep 17 00:00:00 2001 From: yulin2 Date: Mon, 4 May 2015 20:57:20 -0500 Subject: [PATCH 14/14] rename AddDeviceToApiKeyBatch constructor --- src/com/prey/services/AddDeviceToApiKeyBatch.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/prey/services/AddDeviceToApiKeyBatch.java b/src/com/prey/services/AddDeviceToApiKeyBatch.java index 7a671e9a..1150f97c 100644 --- a/src/com/prey/services/AddDeviceToApiKeyBatch.java +++ b/src/com/prey/services/AddDeviceToApiKeyBatch.java @@ -12,8 +12,8 @@ public class AddDeviceToApiKeyBatch extends IntentService { private String error; - public AddDeviceToApiKeyBatch(String name) { - super(name); + public AddDeviceToApiKeyBatch() { + super("AddDeviceToApiKeyBatch"); } public void onHandleIntent(Intent intent) {