1
0
mirror of https://github.com/nerzhul/ownCloud-SMS-App.git synced 2026-08-22 22:03:10 +00:00

Refactor code with Java best practices, helped by AS

This commit is contained in:
Loic Blot
2015-11-03 23:00:31 +01:00
parent 2576ca7468
commit 9d61d6c732
17 changed files with 365 additions and 371 deletions
@@ -17,8 +17,6 @@ package fr.unix_experience.owncloud_sms.activities;
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import java.util.List;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.ContentResolver;
@@ -26,6 +24,9 @@ import android.content.PeriodicSync;
import android.os.Bundle;
import android.preference.ListPreference;
import android.util.Log;
import java.util.List;
import fr.nrz.androidlib.activities.NrzSettingsActivity;
import fr.unix_experience.owncloud_sms.R;
import fr.unix_experience.owncloud_sms.defines.DefaultPrefs;
@@ -39,87 +40,81 @@ public class GeneralSettingsActivity extends NrzSettingsActivity {
private static String _accountType;
@Override
protected void onPostCreate(final Bundle savedInstanceState) {
_accountMgr = AccountManager.get(getBaseContext());
_accountAuthority = getString(R.string.account_authority);
_accountType = getString(R.string.account_type);
_prefsRessourceFile = R.xml.pref_data_sync;
protected void onPostCreate(Bundle savedInstanceState) {
GeneralSettingsActivity._accountMgr = AccountManager.get(getBaseContext());
GeneralSettingsActivity._accountAuthority = getString(R.string.account_authority);
GeneralSettingsActivity._accountType = getString(R.string.account_type);
NrzSettingsActivity._prefsRessourceFile = R.xml.pref_data_sync;
// Bind our boolean preferences
_boolPrefs.add(new BindObjectPref("push_on_receive", DefaultPrefs.pushOnReceive));
_boolPrefs.add(new BindObjectPref("sync_wifi", DefaultPrefs.syncWifi));
_boolPrefs.add(new BindObjectPref("sync_4g", DefaultPrefs.sync4G));
_boolPrefs.add(new BindObjectPref("sync_3g", DefaultPrefs.sync3G));
_boolPrefs.add(new BindObjectPref("sync_gprs", DefaultPrefs.syncGPRS));
_boolPrefs.add(new BindObjectPref("sync_2g", DefaultPrefs.sync2G));
_boolPrefs.add(new BindObjectPref("sync_others", DefaultPrefs.syncOthers));
NrzSettingsActivity._boolPrefs.add(new BindObjectPref("push_on_receive", DefaultPrefs.pushOnReceive));
NrzSettingsActivity._boolPrefs.add(new BindObjectPref("sync_wifi", DefaultPrefs.syncWifi));
NrzSettingsActivity._boolPrefs.add(new BindObjectPref("sync_4g", DefaultPrefs.sync4G));
NrzSettingsActivity._boolPrefs.add(new BindObjectPref("sync_3g", DefaultPrefs.sync3G));
NrzSettingsActivity._boolPrefs.add(new BindObjectPref("sync_gprs", DefaultPrefs.syncGPRS));
NrzSettingsActivity._boolPrefs.add(new BindObjectPref("sync_2g", DefaultPrefs.sync2G));
NrzSettingsActivity._boolPrefs.add(new BindObjectPref("sync_others", DefaultPrefs.syncOthers));
// Bind our string preferences
_stringPrefs.add(new BindObjectPref("sync_frequency", ""));
NrzSettingsActivity._stringPrefs.add(new BindObjectPref("sync_frequency", ""));
// Must be at the end, after preference bind
super.onPostCreate(savedInstanceState);
}
protected static void handleCheckboxPreference(final String key, final Boolean value) {
protected static void handleCheckboxPreference(String key, Boolean value) {
// Network types allowed for sync
if(key.equals(new String("push_on_receive")) ||
key.equals(new String("sync_wifi")) || key.equals("sync_2g") ||
key.equals(new String("sync_3g")) || key.equals("sync_gprs") ||
key.equals("sync_4g") || key.equals("sync_others")) {
final OCSMSSharedPrefs prefs = new OCSMSSharedPrefs(_context);
Log.d(TAG,"GeneralSettingsActivity.handleCheckboxPreference: set " + key + " to "
if("push_on_receive".equals(key) ||
"sync_wifi".equals(key) || "sync_2g".equals(key) ||
"sync_3g".equals(key) || "sync_gprs".equals(key) ||
"sync_4g".equals(key) || "sync_others".equals(key)) {
OCSMSSharedPrefs prefs = new OCSMSSharedPrefs(NrzSettingsActivity._context);
Log.d(GeneralSettingsActivity.TAG,"GeneralSettingsActivity.handleCheckboxPreference: set " + key + " to "
+ value.toString());
prefs.putBoolean(key, value);
}
else {
// Unknown option
}
}
protected static void handleListPreference(final String key, final String value,
final ListPreference preference) {
protected static void handleListPreference(String key, String value,
ListPreference preference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
final int index = preference.findIndexOfValue(value);
int index = preference.findIndexOfValue(value);
// Set the summary to reflect the new value.
preference
.setSummary(index >= 0 ? preference.getEntries()[index]
: null);
.setSummary((index >= 0) ? preference.getEntries()[index]
: null);
// Handle sync frequency change
if (key.equals("sync_frequency")) {
final Account[] myAccountList = _accountMgr.getAccountsByType(_accountType);
final long syncFreq = Long.parseLong(value);
if ("sync_frequency".equals(key)) {
Account[] myAccountList = GeneralSettingsActivity._accountMgr.getAccountsByType(GeneralSettingsActivity._accountType);
long syncFreq = Long.parseLong(value);
// Get ownCloud SMS account list
for (int i = 0; i < myAccountList.length; i++) {
// And get all authorities for this account
final List<PeriodicSync> syncList = ContentResolver.getPeriodicSyncs(myAccountList[i], _accountAuthority);
List<PeriodicSync> syncList = ContentResolver.getPeriodicSyncs(myAccountList[i], GeneralSettingsActivity._accountAuthority);
boolean foundSameSyncCycle = false;
for (int j = 0; j < syncList.size(); j++) {
final PeriodicSync ps = syncList.get(i);
PeriodicSync ps = syncList.get(i);
if (ps.period == syncFreq && ps.extras.getInt("synctype") == 1) {
if ((ps.period == syncFreq) && (ps.extras.getInt("synctype") == 1)) {
foundSameSyncCycle = true;
}
}
if (foundSameSyncCycle == false) {
final Bundle b = new Bundle();
if (!foundSameSyncCycle) {
Bundle b = new Bundle();
b.putInt("synctype", 1);
ContentResolver.removePeriodicSync(myAccountList[i],
_accountAuthority, b);
GeneralSettingsActivity._accountAuthority, b);
ContentResolver.addPeriodicSync(myAccountList[i],
_accountAuthority, b, syncFreq * 60);
GeneralSettingsActivity._accountAuthority, b, syncFreq * 60);
}
}
}
else {
// Unhandled option
}
}
}
@@ -83,7 +83,7 @@ public class LoginActivity extends Activity {
@Override
public boolean onEditorAction(TextView textView, int id,
KeyEvent keyEvent) {
if (id == R.id.oc_login || id == EditorInfo.IME_NULL) {
if ((id == R.id.oc_login) || (id == EditorInfo.IME_NULL)) {
attemptLogin();
return true;
}
@@ -161,12 +161,14 @@ public class LoginActivity extends Activity {
if (cancel) {
// There was an error; don't attempt login and focus the first
// form field with an error.
focusView.requestFocus();
} else {
if (focusView != null) {
focusView.requestFocus();
}
} else {
// Show a progress spinner, and kick off a background task to
// perform the user login attempt.
showProgress(true);
String serverURL = new String(protocol + serverAddr);
String serverURL = protocol + serverAddr;
mAuthTask = new UserLoginTask(serverURL, login, password);
mAuthTask.execute((Void) null);
}
@@ -254,12 +256,12 @@ public class LoginActivity extends Activity {
}
@Override
protected void onPostExecute(final Boolean success) {
protected void onPostExecute(Boolean success) {
mAuthTask = null;
showProgress(false);
if (success) {
String accountType = getIntent().getStringExtra(PARAM_AUTHTOKEN_TYPE);
String accountType = getIntent().getStringExtra(UserLoginTask.PARAM_AUTHTOKEN_TYPE);
if (accountType == null) {
accountType = getString(R.string.account_type);
}
@@ -268,7 +270,7 @@ public class LoginActivity extends Activity {
String accountLabel = _login + "@" + _serverURI.getHost();
// We create the account
final Account account = new Account(accountLabel, accountType);
Account account = new Account(accountLabel, accountType);
Bundle accountBundle = new Bundle();
accountBundle.putString("ocLogin", _login);
accountBundle.putString("ocURI", _serverURI.toString());
@@ -293,7 +295,9 @@ public class LoginActivity extends Activity {
getApplicationContext().startActivity(settingsIntent);
} else {
switch (_returnCode) {
case INVALID_ADDR:
case OK:
break;
case INVALID_ADDR:
_serverView.setError(getString(R.string.error_invalid_server_address));
_serverView.requestFocus();
break;
@@ -331,7 +335,6 @@ public class LoginActivity extends Activity {
private final String _password;
private LoginReturnCode _returnCode;
public static final String PARAM_AUTHTOKEN_TYPE = "auth.token";
public static final String PARAM_CREATE = "create";
}
public static final String PARAM_AUTHTOKEN_TYPE = "auth.token";
}
}
@@ -25,11 +25,6 @@ package fr.unix_experience.owncloud_sms.activities;
* SUCH DAMAGE.
*/
import java.util.List;
import java.util.Vector;
import org.json.JSONArray;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
@@ -45,6 +40,12 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import org.json.JSONArray;
import java.util.List;
import java.util.Vector;
import fr.unix_experience.owncloud_sms.R;
import fr.unix_experience.owncloud_sms.activities.remote_account.AccountListActivity;
import fr.unix_experience.owncloud_sms.engine.ASyncSMSSync.SyncTask;
@@ -69,14 +70,14 @@ public class MainActivity extends Activity {
ViewPager mViewPager;
@Override
protected void onCreate(final Bundle savedInstanceState) {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
final List<Fragment> fragments = new Vector<Fragment>();
List<Fragment> fragments = new Vector<>();
/*
* Add the Main tabs here
@@ -101,13 +102,13 @@ public class MainActivity extends Activity {
private final List<Fragment> mFragments;
public MainPagerAdapter(final FragmentManager fragmentManager, final List<Fragment> fragments) {
public MainPagerAdapter(FragmentManager fragmentManager, List<Fragment> fragments) {
super(fragmentManager);
mFragments = fragments;
}
@Override
public Fragment getItem(final int position) {
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class
// below).
@@ -126,53 +127,50 @@ public class MainActivity extends Activity {
*/
public static class StarterFragment extends Fragment {
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
final Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_mainactivity_main, container,
false);
return rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_mainactivity_main, container,
false);
}
}
public static class SecondTestFragment extends Fragment {
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
final Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_mainactivity_gotosettings, container,
false);
return rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_mainactivity_gotosettings, container,
false);
}
}
public static class ThanksAndRateFragment extends Fragment {
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
final Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_mainactivity_thanks_note, container,
false);
return rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_mainactivity_thanks_note, container,
false);
}
}
public void openAppSettings(final View view) {
public void openAppSettings(View view) {
startActivity(new Intent(this, GeneralSettingsActivity.class));
}
public void openAddAccount(final View view) {
public void openAddAccount(View view) {
startActivity(new Intent(Settings.ACTION_ADD_ACCOUNT));
}
public void syncAllMessages(final View view) {
final Context ctx = getApplicationContext();
final ConnectivityMonitor cMon = new ConnectivityMonitor(ctx);
public void syncAllMessages(View view) {
Context ctx = getApplicationContext();
ConnectivityMonitor cMon = new ConnectivityMonitor(ctx);
if (cMon.isValid()) {
// Now fetch messages since last stored date
final JSONArray smsList = new SmsFetcher(ctx)
JSONArray smsList = new SmsFetcher(ctx)
.bufferMessagesSinceDate((long) 0);
if (smsList != null) {
final OCSMSNotificationManager nMgr = new OCSMSNotificationManager(ctx);
OCSMSNotificationManager nMgr = new OCSMSNotificationManager(ctx);
nMgr.setSyncProcessMsg();
new SyncTask(getApplicationContext(), smsList).execute();
}
@@ -182,16 +180,16 @@ public class MainActivity extends Activity {
}
}
public void selectRemoteAccount(final View view) {
public void selectRemoteAccount(View view) {
startActivity(new Intent(this, AccountListActivity.class));
}
public void openGooglePlayStore(final View view) {
public void openGooglePlayStore(View view) {
Intent intent;
try {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + getPackageName()));
} catch (final android.content.ActivityNotFoundException anfe) {
} catch (android.content.ActivityNotFoundException anfe) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + getPackageName()));
}
@@ -1,23 +1,25 @@
package fr.unix_experience.owncloud_sms.activities.remote_account;
import java.util.ArrayList;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.ListActivity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.Collections;
import fr.nrz.androidlib.adapters.AndroidAccountAdapter;
import fr.unix_experience.owncloud_sms.R;
public class AccountListActivity extends ListActivity {
ArrayList<Account> listItems = new ArrayList<Account>();
ArrayList<Account> listItems = new ArrayList<>();
AndroidAccountAdapter adapter;
@Override
public void onCreate(final Bundle icicle) {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
final AccountManager _accountMgr = AccountManager.get(getBaseContext());
AccountManager _accountMgr = AccountManager.get(getBaseContext());
setContentView(R.layout.restore_activity_accountlist);
adapter = new AndroidAccountAdapter(this,
@@ -27,11 +29,9 @@ public class AccountListActivity extends ListActivity {
R.id.accountname, ContactListActivity.class);
setListAdapter(adapter);
final Account[] accountList =
Account[] accountList =
_accountMgr.getAccountsByType(getString(R.string.account_type));
for (final Account element : accountList) {
listItems.add(element);
}
Collections.addAll(listItems, accountList);
adapter.notifyDataSetChanged();
}
@@ -30,19 +30,19 @@ public class ContactListActivity extends Activity implements ASyncContactLoad {
ArrayList<String> objects;
@Override
protected void onCreate(final Bundle savedInstanceState) {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
assert getIntent().getExtras() != null;
final String accountName = getIntent().getExtras().getString("account");
String accountName = getIntent().getExtras().getString("account");
// accountName cannot be null, devel error
assert accountName != null;
_accountMgr = AccountManager.get(getBaseContext());
final Account[] myAccountList =
_accountMgr.getAccountsByType(getString(R.string.account_type));
ContactListActivity._accountMgr = AccountManager.get(getBaseContext());
Account[] myAccountList =
ContactListActivity._accountMgr.getAccountsByType(getString(R.string.account_type));
// Init view
objects = new ArrayList<>();
@@ -79,8 +79,8 @@ public class ContactListActivity extends Activity implements ASyncContactLoad {
Integer smsCount = 0;
// @TODO asynctask to load more datas
if (phoneList.size() > 0) {
String res = new String("");
if (!phoneList.isEmpty()) {
String res = "";
for (String pn: phoneList) {
res += "- " + pn + "\n";
}
@@ -101,28 +101,34 @@ public class ContactListActivity extends Activity implements ASyncContactLoad {
Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " = ?",
new String[]{name}, null);
people.moveToFirst();
if (people == null) {
return new Vector<>();
}
Vector<String> r = new Vector<>();
people.moveToFirst();
Vector<String> r = new Vector<>();
if (people.getCount() == 0) {
return r;
}
String contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID));
if (people.getString(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))
.equalsIgnoreCase("1")) {
if ("1".equalsIgnoreCase(people.getString(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))) {
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{contactId}, null);
while (phones.moveToNext()) {
while ((phones != null) && phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
.replaceAll(" ", "");
r.add(phoneNumber);
}
phones.close();
}
if (phones != null) {
phones.close();
}
}
return r;
}
});