Update sync to Google Drive setting

This commit is contained in:
Jimmy Shih
2014-01-07 17:02:02 -08:00
parent 76e44794ff
commit 29cffa96e6
10 changed files with 348 additions and 450 deletions
-1
View File
@@ -73,7 +73,6 @@ limitations under the License.
<!-- Keys for non-persisted preferences. Sorted alphabetically. DO NOT TRANSLATE. --> <!-- Keys for non-persisted preferences. Sorted alphabetically. DO NOT TRANSLATE. -->
<string name="settings_advanced_key">settingsAdvanced</string> <string name="settings_advanced_key">settingsAdvanced</string>
<string name="settings_chart_key">settingsChart</string> <string name="settings_chart_key">settingsChart</string>
<string name="settings_google_key">settingsGoogle</string>
<string name="settings_map_key">settingsMap</string> <string name="settings_map_key">settingsMap</string>
<string name="settings_map_medium_display_key">settingsMapMediumDisplay</string> <string name="settings_map_medium_display_key">settingsMapMediumDisplay</string>
<string name="settings_map_percentage_display_key">settingsMapPercentageDisplay</string> <string name="settings_map_percentage_display_key">settingsMapPercentageDisplay</string>
+6 -29
View File
@@ -1553,37 +1553,14 @@ limitations under the License.
</string> </string>
<!-- Settings Google --> <!-- Settings Google -->
<string name="settings_google" <string name="settings_google_drive_sync_no_account"
translation_description="In the 'Settings' page, the option to change the accounts and sync translation_description="In the 'Sync to Google Drive' setting, the message when no account is
settings. found to sync to Google Drive.">
No account found to sync to Google Drive
Character limit: ~23 (1 line)">
Accounts &amp; sync
</string>
<string name="settings_google_account_confirm_message"
translation_description="In the 'Accounts and sync' settings, the confirmation message when
switching account.
%1$s account name. E.g., 'foo@foo.com'.">
This will remove any association between your tracks and %1$s Google Drive. Google Drive sync
will be turned off. No tracks will be deleted on %1$s Google Drive.
</string>
<string name="settings_google_account_confirm_title"
translation_description="In the 'Accounts and sync' settings, after pressing 'Switch account',
the title of the confirmation dialog.">
Switch account?
</string>
<string name="settings_google_account_title"
translation_description="In the 'Accounts and sync' settings, the option to switch Google
account.
Character limit: ~23 (1 line)">
Switch account
</string> </string>
<string name="settings_google_drive_sync_summary_on" <string name="settings_google_drive_sync_summary_on"
translation_description="In the 'Accounts and sync' settings, the summary for the 'Sync to translation_description="In the 'Sync to Google Drive' setting, the summary explaining that all
Google Drive' checkbox when it is checked explaining that all tracks will be synced to a tracks will be synced to a Google Drive account.
Google Drive account.
%1$s account name. E.g., 'foo@foo.com'."> %1$s account name. E.g., 'foo@foo.com'.">
All tracks will be synced to %1$s Google Drive All tracks will be synced to %1$s Google Drive
-27
View File
@@ -1,27 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2013 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/settings_google" >
<com.google.android.apps.mytracks.settings.MyTracksListPreference
android:defaultValue=""
android:key="@string/google_account_key"
android:title="@string/settings_google_account_title" />
<CheckBoxPreference
android:defaultValue="false"
android:key="@string/drive_sync_key"
android:title="@string/menu_sync_drive" />
</PreferenceScreen>
+4 -4
View File
@@ -16,10 +16,10 @@ limitations under the License.
--> -->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/menu_settings" > android:title="@string/menu_settings" >
<Preference <CheckBoxPreference
android:key="@string/settings_google_key" android:defaultValue="false"
android:persistent="false" android:key="@string/drive_sync_key"
android:title="@string/settings_google" /> android:title="@string/menu_sync_drive" />
<Preference <Preference
android:key="@string/settings_map_key" android:key="@string/settings_map_key"
android:persistent="false" android:persistent="false"
@@ -25,7 +25,6 @@ import android.app.Dialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnClickListener;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
/** /**
* A DialogFrament to enable sync to Google Drive. * A DialogFrament to enable sync to Google Drive.
@@ -1,280 +0,0 @@
/*
* Copyright 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.android.apps.mytracks.settings;
import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.io.sendtogoogle.SendToGoogleUtils;
import com.google.android.apps.mytracks.io.sync.SyncUtils;
import com.google.android.apps.mytracks.services.tasks.CheckPermissionAsyncTask;
import com.google.android.apps.mytracks.services.tasks.CheckPermissionAsyncTask.CheckPermissionCaller;
import com.google.android.apps.mytracks.util.DialogUtils;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.maps.mytracks.R;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
* An activity for accessing the Google settings.
*
* @author Jimmy Shih
*/
public class GoogleSettingsActivity extends AbstractSettingsActivity
implements CheckPermissionCaller {
private static final String ACCOUNT_NAME_KEY = "accountName";
private static final int DIALOG_CONFIRM_SWITCH_ACCOUNT = 0;
private static final int DIALOG_CONFIRM_DRIVE_SYNC_ON = 1;
private static final int DRIVE_REQUEST_CODE = 0;
private ListPreference googleAccountPreference;
private CheckBoxPreference driveSyncPreference;
private CheckPermissionAsyncTask asyncTask;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
Object retained = getLastNonConfigurationInstance();
if (retained instanceof CheckPermissionAsyncTask) {
asyncTask = (CheckPermissionAsyncTask) retained;
asyncTask.setActivity(this);
}
addPreferencesFromResource(R.xml.google_settings);
googleAccountPreference = (ListPreference) findPreference(
getString(R.string.google_account_key));
List<String> entries = new ArrayList<String>();
List<String> entryValues = new ArrayList<String>();
Account[] accounts = AccountManager.get(this).getAccountsByType(Constants.ACCOUNT_TYPE);
for (Account account : accounts) {
entries.add(account.name);
entryValues.add(account.name);
}
entries.add(getString(R.string.value_none));
entryValues.add(PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
googleAccountPreference.setEntries(entries.toArray(new CharSequence[entries.size()]));
googleAccountPreference.setEntryValues(entryValues.toArray(new CharSequence[entries.size()]));
googleAccountPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String newGoogleAccount = (String) newValue;
String googleAccount = PreferencesUtils.getString(
GoogleSettingsActivity.this, R.string.google_account_key,
PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
if (googleAccount == null
|| googleAccount.equals(PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT)) {
updateUiByAccountName(newGoogleAccount);
return true;
}
if (!googleAccount.equals(newGoogleAccount)) {
Bundle newBundle = new Bundle();
newBundle.putString(ACCOUNT_NAME_KEY, newGoogleAccount);
showDialog(DIALOG_CONFIRM_SWITCH_ACCOUNT, newBundle);
}
return false;
}
});
driveSyncPreference = (CheckBoxPreference) findPreference(getString(R.string.drive_sync_key));
driveSyncPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if ((Boolean) newValue) {
showDialog(DIALOG_CONFIRM_DRIVE_SYNC_ON);
} else {
handleSync(false);
}
return false;
}
});
updateUiByAccountName(PreferencesUtils.getString(
this, R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT));
}
@Deprecated
public Object onRetainNonConfigurationInstance() {
if (asyncTask != null) {
asyncTask.setActivity(null);
}
return asyncTask;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case DRIVE_REQUEST_CODE:
SendToGoogleUtils.cancelNotification(this, SendToGoogleUtils.DRIVE_NOTIFICATION_ID);
if (resultCode == Activity.RESULT_OK) {
onDrivePermissionSuccess();
} else {
onDrivePermissionFailure();
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
}
}
@Override
protected Dialog onCreateDialog(int id, Bundle bundle) {
Dialog dialog;
switch (id) {
case DIALOG_CONFIRM_SWITCH_ACCOUNT:
dialog = DialogUtils.createConfirmationDialog(this,
R.string.settings_google_account_confirm_title,
getString(R.string.settings_google_account_confirm_message), null);
break;
case DIALOG_CONFIRM_DRIVE_SYNC_ON:
dialog = DialogUtils.createConfirmationDialog(this, R.string.sync_drive_title,
getString(R.string.sync_drive_confirm_message), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface d, int button) {
if (asyncTask == null) {
final String googleAccount = PreferencesUtils.getString(
GoogleSettingsActivity.this, R.string.google_account_key,
PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
asyncTask = new CheckPermissionAsyncTask(
GoogleSettingsActivity.this, googleAccount, SendToGoogleUtils.DRIVE_SCOPE);
asyncTask.execute();
}
}
});
break;
default:
dialog = null;
}
return dialog;
}
@Override
public void onCheckPermissionDone(String scope, boolean success, Intent userRecoverableIntent) {
asyncTask = null;
if (success) {
onDrivePermissionSuccess();
} else {
if (userRecoverableIntent != null) {
startActivityForResult(userRecoverableIntent, DRIVE_REQUEST_CODE);
} else {
onDrivePermissionFailure();
}
}
}
private void onDrivePermissionSuccess() {
handleSync(true);
}
private void onDrivePermissionFailure() {
Toast.makeText(
GoogleSettingsActivity.this, R.string.send_google_no_account_message, Toast.LENGTH_LONG)
.show();
}
@Override
protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
AlertDialog alertDialog = (AlertDialog) dialog;
String googleAccount = PreferencesUtils.getString(
this, R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
switch (id) {
case DIALOG_CONFIRM_SWITCH_ACCOUNT:
final String newValue = bundle.getString(ACCOUNT_NAME_KEY);
alertDialog.setMessage(
getString(R.string.settings_google_account_confirm_message, googleAccount));
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, getString(R.string.generic_yes),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface d, int button) {
googleAccountPreference.setValue(newValue);
handleSync(false);
updateUiByAccountName(newValue);
}
});
break;
case DIALOG_CONFIRM_DRIVE_SYNC_ON:
alertDialog.setMessage(getString(R.string.sync_drive_confirm_message, googleAccount,
getString(R.string.my_tracks_app_name)));
break;
default:
}
super.onPrepareDialog(id, dialog, bundle);
}
/**
* Handles sync.
*
* @param value true to sync
*/
private void handleSync(boolean value) {
driveSyncPreference.setChecked(value);
// Turn off everything
SyncUtils.disableSync(this);
if (value) {
// Turn on sync
ContentResolver.setMasterSyncAutomatically(true);
// Enable sync for account
String googleAccount = PreferencesUtils.getString(
this, R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
Account[] accounts = AccountManager.get(this).getAccountsByType(Constants.ACCOUNT_TYPE);
for (Account account : accounts) {
if (account.name.equals(googleAccount)) {
SyncUtils.enableSync(account);
break;
}
}
} else {
SyncUtils.clearSyncState(this);
}
}
/**
* Updates UI by account.
*
* @param accountName the account name
*/
private void updateUiByAccountName(String accountName) {
boolean hasAccount = !PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT.equals(accountName);
googleAccountPreference.setSummary(
hasAccount ? accountName : getString(R.string.value_unknown));
driveSyncPreference.setEnabled(hasAccount);
driveSyncPreference.setSummaryOn(
getString(R.string.settings_google_drive_sync_summary_on, accountName));
}
}
@@ -16,29 +16,90 @@
package com.google.android.apps.mytracks.settings; package com.google.android.apps.mytracks.settings;
import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.io.sendtogoogle.SendToGoogleUtils;
import com.google.android.apps.mytracks.io.sync.SyncUtils;
import com.google.android.apps.mytracks.services.tasks.CheckPermissionAsyncTask;
import com.google.android.apps.mytracks.services.tasks.CheckPermissionAsyncTask.CheckPermissionCaller;
import com.google.android.apps.mytracks.util.DialogUtils;
import com.google.android.apps.mytracks.util.IntentUtils; import com.google.android.apps.mytracks.util.IntentUtils;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference; import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener; import android.preference.Preference.OnPreferenceClickListener;
import android.widget.Toast;
/** /**
* An activity for accessing settings. * An activity for accessing settings.
* *
* @author Leif Hendrik Wilden * @author Jimmy Shih
* @author Rodrigo Damazio
*/ */
public class SettingsActivity extends AbstractSettingsActivity { public class SettingsActivity extends AbstractSettingsActivity implements CheckPermissionCaller {
private static final int DIALOG_NO_ACCOUNT = 0;
private static final int DIALOG_CHOOSE_ACCOUNT = 1;
private static final int DIALOG_CONFIRM_DRIVE_SYNC_ON = 2;
private static final int DRIVE_REQUEST_CODE = 0;
private CheckBoxPreference syncDrivePreference;
private CheckPermissionAsyncTask syncDriveAsyncTask;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Override @Override
protected void onCreate(Bundle bundle) { protected void onCreate(Bundle bundle) {
super.onCreate(bundle); super.onCreate(bundle);
Object retained = getLastNonConfigurationInstance();
if (retained instanceof CheckPermissionAsyncTask) {
syncDriveAsyncTask = (CheckPermissionAsyncTask) retained;
syncDriveAsyncTask.setActivity(this);
}
addPreferencesFromResource(R.xml.settings); addPreferencesFromResource(R.xml.settings);
configPreference(R.string.settings_google_key, GoogleSettingsActivity.class); syncDrivePreference = (CheckBoxPreference) findPreference(getString(R.string.drive_sync_key));
syncDrivePreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if ((Boolean) newValue) {
Account[] accounts = AccountManager.get(SettingsActivity.this)
.getAccountsByType(Constants.ACCOUNT_TYPE);
if (accounts.length == 0) {
PreferencesUtils.setString(SettingsActivity.this, R.string.google_account_key,
PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
handleSync(false);
showDialog(DIALOG_NO_ACCOUNT);
} else if (accounts.length == 1) {
PreferencesUtils.setString(
SettingsActivity.this, R.string.google_account_key, accounts[0].name);
showDialog(DIALOG_CONFIRM_DRIVE_SYNC_ON);
} else {
showDialog(DIALOG_CHOOSE_ACCOUNT);
}
} else {
PreferencesUtils.setString(SettingsActivity.this, R.string.google_account_key,
PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
handleSync(false);
}
return false;
}
});
updateSyncDriveSummary();
configPreference(R.string.settings_map_key, MapSettingsActivity.class); configPreference(R.string.settings_map_key, MapSettingsActivity.class);
configPreference(R.string.settings_chart_key, ChartSettingsActivity.class); configPreference(R.string.settings_chart_key, ChartSettingsActivity.class);
configPreference(R.string.settings_stats_key, StatsSettingsActivity.class); configPreference(R.string.settings_stats_key, StatsSettingsActivity.class);
@@ -47,6 +108,177 @@ public class SettingsActivity extends AbstractSettingsActivity {
configPreference(R.string.settings_advanced_key, AdvancedSettingsActivity.class); configPreference(R.string.settings_advanced_key, AdvancedSettingsActivity.class);
} }
@Deprecated
public Object onRetainNonConfigurationInstance() {
if (syncDriveAsyncTask != null) {
syncDriveAsyncTask.setActivity(null);
}
return syncDriveAsyncTask;
}
@Override
protected Dialog onCreateDialog(int id, Bundle bundle) {
final Dialog dialog;
switch (id) {
case DIALOG_NO_ACCOUNT:
dialog = new AlertDialog.Builder(this).setMessage(
R.string.settings_google_drive_sync_no_account)
.setTitle(R.string.send_google_no_account_title)
.setPositiveButton(R.string.generic_ok, null).create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
DialogUtils.setDialogTitleDivider(SettingsActivity.this, dialog);
}
});
break;
case DIALOG_CHOOSE_ACCOUNT:
Account[] accounts = AccountManager.get(SettingsActivity.this)
.getAccountsByType(Constants.ACCOUNT_TYPE);
final String[] choices = new String[accounts.length];
for (int i = 0; i < accounts.length; i++) {
choices[i] = accounts[i].name;
}
dialog = new AlertDialog.Builder(this).setNegativeButton(R.string.generic_cancel, null)
.setPositiveButton(R.string.generic_ok, new OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(DialogInterface dialogInterface, int which) {
int position = ((AlertDialog) dialogInterface).getListView()
.getCheckedItemPosition();
PreferencesUtils.setString(
SettingsActivity.this, R.string.google_account_key, choices[position]);
dismissDialog(DIALOG_CHOOSE_ACCOUNT);
showDialog(DIALOG_CONFIRM_DRIVE_SYNC_ON);
}
}).setSingleChoiceItems(choices, 0, null)
.setTitle(R.string.send_google_choose_account_title).create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
DialogUtils.setDialogTitleDivider(SettingsActivity.this, dialog);
}
});
break;
case DIALOG_CONFIRM_DRIVE_SYNC_ON:
dialog = DialogUtils.createConfirmationDialog(this, R.string.sync_drive_title,
getString(R.string.sync_drive_confirm_message), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface d, int button) {
if (syncDriveAsyncTask == null) {
String googleAccount = PreferencesUtils.getString(SettingsActivity.this,
R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
syncDriveAsyncTask = new CheckPermissionAsyncTask(
SettingsActivity.this, googleAccount, SendToGoogleUtils.DRIVE_SCOPE);
syncDriveAsyncTask.execute();
}
}
});
break;
default:
dialog = null;
}
return dialog;
}
@SuppressWarnings("deprecation")
@Override
protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
if (id == DIALOG_CONFIRM_DRIVE_SYNC_ON) {
AlertDialog alertDialog = (AlertDialog) dialog;
String googleAccount = PreferencesUtils.getString(
this, R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
alertDialog.setMessage(getString(R.string.sync_drive_confirm_message, googleAccount,
getString(R.string.my_tracks_app_name)));
}
super.onPrepareDialog(id, dialog, bundle);
}
@Override
public void onCheckPermissionDone(String scope, boolean success, Intent userRecoverableIntent) {
syncDriveAsyncTask = null;
if (success) {
onDrivePermissionSuccess();
} else {
if (userRecoverableIntent != null) {
startActivityForResult(userRecoverableIntent, DRIVE_REQUEST_CODE);
} else {
onDrivePermissionFailure();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case DRIVE_REQUEST_CODE:
SendToGoogleUtils.cancelNotification(this, SendToGoogleUtils.DRIVE_NOTIFICATION_ID);
if (resultCode == Activity.RESULT_OK) {
onDrivePermissionSuccess();
} else {
onDrivePermissionFailure();
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
}
}
private void onDrivePermissionSuccess() {
handleSync(true);
}
private void onDrivePermissionFailure() {
Toast.makeText(
SettingsActivity.this, R.string.settings_google_drive_sync_no_account, Toast.LENGTH_LONG)
.show();
}
/**
* Handles sync.
*
* @param value true to sync
*/
private void handleSync(boolean value) {
syncDrivePreference.setChecked(value);
updateSyncDriveSummary();
// Turn off everything
SyncUtils.disableSync(this);
if (value) {
// Turn on sync
ContentResolver.setMasterSyncAutomatically(true);
// Enable sync for account
String googleAccount = PreferencesUtils.getString(
this, R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
Account[] accounts = AccountManager.get(this).getAccountsByType(Constants.ACCOUNT_TYPE);
for (Account account : accounts) {
if (account.name.equals(googleAccount)) {
SyncUtils.enableSync(account);
break;
}
}
} else {
SyncUtils.clearSyncState(this);
}
}
/**
* Updates UI by account.
*/
private void updateSyncDriveSummary() {
String googleAccount = PreferencesUtils.getString(
this, R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
boolean hasAccount = !PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT.equals(googleAccount);
syncDrivePreference.setSummaryOn(
hasAccount ? getString(R.string.settings_google_drive_sync_summary_on, googleAccount) : "");
}
/** /**
* Configures a preference by starting a new activity when it is clicked. * Configures a preference by starting a new activity when it is clicked.
* *
@@ -15,7 +15,6 @@
*/ */
package com.google.android.apps.mytracks.endtoendtest; package com.google.android.apps.mytracks.endtoendtest;
import com.google.android.maps.mytracks.R;
/** /**
* Places all the test configuration in this class. * Places all the test configuration in this class.
@@ -51,20 +50,21 @@ public class RunConfiguration {
* @return true means can run sync tests in this device * @return true means can run sync tests in this device
*/ */
public static boolean canRunSyncTest() { public static boolean canRunSyncTest() {
EndToEndTestUtils.findMenuItem( return true;
EndToEndTestUtils.activityMytracks.getString(R.string.menu_settings), true); // EndToEndTestUtils.findMenuItem(
EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.activityMytracks // EndToEndTestUtils.activityMytracks.getString(R.string.menu_settings), true);
.getString(R.string.settings_google)); // EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.activityMytracks
EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.activityMytracks // .getString(R.string.settings_google));
.getString(R.string.settings_google_account_title)); // EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.activityMytracks
boolean canRunSyncE2ETest = EndToEndTestUtils.SOLO.waitForText(GoogleUtils.ACCOUNT_NAME_1, 1, // .getString(R.string.settings_google_account_title));
EndToEndTestUtils.SHORT_WAIT_TIME) // boolean canRunSyncE2ETest = EndToEndTestUtils.SOLO.waitForText(GoogleUtils.ACCOUNT_NAME_1, 1,
&& EndToEndTestUtils.SOLO.waitForText(GoogleUtils.ACCOUNT_NAME_2, 1, // EndToEndTestUtils.SHORT_WAIT_TIME)
EndToEndTestUtils.TINY_WAIT_TIME); // && EndToEndTestUtils.SOLO.waitForText(GoogleUtils.ACCOUNT_NAME_2, 1,
EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.activityMytracks // EndToEndTestUtils.TINY_WAIT_TIME);
.getString(R.string.generic_cancel)); // EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.activityMytracks
EndToEndTestUtils.SOLO.goBack(); // .getString(R.string.generic_cancel));
EndToEndTestUtils.SOLO.goBack(); // EndToEndTestUtils.SOLO.goBack();
return canRunSyncE2ETest; // EndToEndTestUtils.SOLO.goBack();
// return canRunSyncE2ETest;
} }
} }
@@ -18,7 +18,6 @@ package com.google.android.apps.mytracks.endtoendtest.common;
import com.google.android.apps.mytracks.ChartView; import com.google.android.apps.mytracks.ChartView;
import com.google.android.apps.mytracks.TrackListActivity; import com.google.android.apps.mytracks.TrackListActivity;
import com.google.android.apps.mytracks.endtoendtest.EndToEndTestUtils; import com.google.android.apps.mytracks.endtoendtest.EndToEndTestUtils;
import com.google.android.apps.mytracks.endtoendtest.GoogleUtils;
import com.google.android.apps.mytracks.util.PreferencesUtils; import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
@@ -363,48 +362,48 @@ public class SettingTest extends ActivityInstrumentationTestCase2<TrackListActiv
PreferencesUtils.getString(context, R.string.track_color_mode_key, errorString)); PreferencesUtils.getString(context, R.string.track_color_mode_key, errorString));
} }
/** // /**
* Tests the setting and menu item of sync. // * Tests the setting and menu item of sync.
*/ // */
public void testSyncNowMenu() { // public void testSyncNowMenu() {
EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_settings), true); // EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_settings), true);
EndToEndTestUtils.SOLO.clickOnText(activityMyTracks.getString(R.string.settings_google)); // EndToEndTestUtils.SOLO.clickOnText(activityMyTracks.getString(R.string.settings_google));
EndToEndTestUtils.SOLO.clickOnText(activityMyTracks // EndToEndTestUtils.SOLO.clickOnText(activityMyTracks
.getString(R.string.settings_google_account_title)); // .getString(R.string.settings_google_account_title));
//
// Whether test account is bound. // // Whether test account is bound.
if (EndToEndTestUtils.SOLO.waitForText(GoogleUtils.ACCOUNT_NAME_1, 1, // if (EndToEndTestUtils.SOLO.waitForText(GoogleUtils.ACCOUNT_NAME_1, 1,
EndToEndTestUtils.TINY_WAIT_TIME)) { // EndToEndTestUtils.TINY_WAIT_TIME)) {
EndToEndTestUtils.SOLO.clickOnText(GoogleUtils.ACCOUNT_NAME_1); // EndToEndTestUtils.SOLO.clickOnText(GoogleUtils.ACCOUNT_NAME_1);
instrumentation.waitForIdleSync(); // instrumentation.waitForIdleSync();
EndToEndTestUtils.SOLO.waitForText(activityMyTracks.getString(R.string.menu_sync_drive)); // EndToEndTestUtils.SOLO.waitForText(activityMyTracks.getString(R.string.menu_sync_drive));
assertTrue(EndToEndTestUtils.findTextView( // assertTrue(EndToEndTestUtils.findTextView(
activityMyTracks.getString(R.string.menu_sync_drive)).isEnabled()); // activityMyTracks.getString(R.string.menu_sync_drive)).isEnabled());
} else { // } else {
EndToEndTestUtils.SOLO.clickOnText(activityMyTracks.getString(R.string.value_none)); // EndToEndTestUtils.SOLO.clickOnText(activityMyTracks.getString(R.string.value_none));
instrumentation.waitForIdleSync(); // instrumentation.waitForIdleSync();
EndToEndTestUtils.SOLO.waitForText(activityMyTracks.getString(R.string.menu_sync_drive)); // EndToEndTestUtils.SOLO.waitForText(activityMyTracks.getString(R.string.menu_sync_drive));
assertFalse(EndToEndTestUtils.findTextView( // assertFalse(EndToEndTestUtils.findTextView(
activityMyTracks.getString(R.string.menu_sync_drive)).isEnabled()); // activityMyTracks.getString(R.string.menu_sync_drive)).isEnabled());
return; // return;
} // }
//
EndToEndTestUtils.SOLO.clickOnText(activityMyTracks.getString(R.string.menu_sync_drive)); // EndToEndTestUtils.SOLO.clickOnText(activityMyTracks.getString(R.string.menu_sync_drive));
//
boolean isSyncChecked = false; // boolean isSyncChecked = false;
if (EndToEndTestUtils.SOLO.waitForText( // if (EndToEndTestUtils.SOLO.waitForText(
activityMyTracks.getString(R.string.sync_drive_confirm_message).split("%")[0], 1, // activityMyTracks.getString(R.string.sync_drive_confirm_message).split("%")[0], 1,
EndToEndTestUtils.SHORT_WAIT_TIME)) { // EndToEndTestUtils.SHORT_WAIT_TIME)) {
isSyncChecked = true; // isSyncChecked = true;
} else { // } else {
isSyncChecked = false; // isSyncChecked = false;
} // }
EndToEndTestUtils.SOLO.clickOnText(activityMyTracks.getString(R.string.generic_yes)); // EndToEndTestUtils.SOLO.clickOnText(activityMyTracks.getString(R.string.generic_yes));
EndToEndTestUtils.SOLO.goBack(); // EndToEndTestUtils.SOLO.goBack();
EndToEndTestUtils.SOLO.goBack(); // EndToEndTestUtils.SOLO.goBack();
assertEquals(isSyncChecked, // assertEquals(isSyncChecked,
EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_sync_now), false)); // EndToEndTestUtils.findMenuItem(activityMyTracks.getString(R.string.menu_sync_now), false));
} // }
@Override @Override
protected void tearDown() throws Exception { protected void tearDown() throws Exception {
@@ -34,7 +34,6 @@ import com.google.api.services.drive.model.File;
import android.app.Instrumentation; import android.app.Instrumentation;
import android.content.Context; import android.content.Context;
import android.util.Log; import android.util.Log;
import android.widget.CheckBox;
import android.widget.ListView; import android.widget.ListView;
import java.io.BufferedReader; import java.io.BufferedReader;
@@ -174,50 +173,50 @@ public class SyncTestUtils {
* @param accountName the account which is used to sync with * @param accountName the account which is used to sync with
*/ */
public static void enableSync(String accountName) { public static void enableSync(String accountName) {
EndToEndTestUtils.instrumentation.waitForIdleSync(); // EndToEndTestUtils.instrumentation.waitForIdleSync();
EndToEndTestUtils.findMenuItem( // EndToEndTestUtils.findMenuItem(
EndToEndTestUtils.activityMytracks.getString(R.string.menu_settings), true); // EndToEndTestUtils.activityMytracks.getString(R.string.menu_settings), true);
EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.activityMytracks // EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.activityMytracks
.getString(R.string.settings_google)); // .getString(R.string.settings_google));
EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.activityMytracks // EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.activityMytracks
.getString(R.string.settings_google_account_title)); // .getString(R.string.settings_google_account_title));
EndToEndTestUtils.instrumentation.waitForIdleSync(); // EndToEndTestUtils.instrumentation.waitForIdleSync();
//
// Whether test account is bound. // // Whether test account is bound.
if (EndToEndTestUtils.SOLO.waitForText(accountName, 1, EndToEndTestUtils.SHORT_WAIT_TIME)) { // if (EndToEndTestUtils.SOLO.waitForText(accountName, 1, EndToEndTestUtils.SHORT_WAIT_TIME)) {
EndToEndTestUtils.SOLO.clickOnText(accountName); // EndToEndTestUtils.SOLO.clickOnText(accountName);
EndToEndTestUtils.instrumentation.waitForIdleSync(); // EndToEndTestUtils.instrumentation.waitForIdleSync();
//
if (EndToEndTestUtils.findTextView(EndToEndTestUtils.activityMytracks // if (EndToEndTestUtils.findTextView(EndToEndTestUtils.activityMytracks
.getString(R.string.settings_google_account_confirm_title)) != null) { // .getString(R.string.settings_google_account_confirm_title)) != null) {
EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.activityMytracks // EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.activityMytracks
.getString(R.string.generic_yes)); // .getString(R.string.generic_yes));
} // }
} else { // } else {
Assert.fail(); // Assert.fail();
return; // return;
} // }
//
EndToEndTestUtils.SOLO.waitForDialogToClose(EndToEndTestUtils.SHORT_WAIT_TIME); // EndToEndTestUtils.SOLO.waitForDialogToClose(EndToEndTestUtils.SHORT_WAIT_TIME);
EndToEndTestUtils.instrumentation.waitForIdleSync(); // EndToEndTestUtils.instrumentation.waitForIdleSync();
CheckBox syncCheckBox = EndToEndTestUtils.SOLO.getCurrentViews(CheckBox.class).get(0); // CheckBox syncCheckBox = EndToEndTestUtils.SOLO.getCurrentViews(CheckBox.class).get(0);
if (!syncCheckBox.isChecked()) { // if (!syncCheckBox.isChecked()) {
EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.activityMytracks // EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.activityMytracks
.getString(R.string.menu_sync_drive)); // .getString(R.string.menu_sync_drive));
//
EndToEndTestUtils.SOLO.waitForText( // EndToEndTestUtils.SOLO.waitForText(
EndToEndTestUtils.activityMytracks.getString(R.string.sync_drive_confirm_message).split( // EndToEndTestUtils.activityMytracks.getString(R.string.sync_drive_confirm_message).split(
"%")[0], 1, EndToEndTestUtils.SHORT_WAIT_TIME); // "%")[0], 1, EndToEndTestUtils.SHORT_WAIT_TIME);
Assert.assertTrue(EndToEndTestUtils.SOLO.searchText(accountName, true)); // Assert.assertTrue(EndToEndTestUtils.SOLO.searchText(accountName, true));
EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.activityMytracks // EndToEndTestUtils.SOLO.clickOnText(EndToEndTestUtils.activityMytracks
.getString(R.string.generic_yes)); // .getString(R.string.generic_yes));
} // }
//
// Add this sleep to work around a exception after switch account. // // Add this sleep to work around a exception after switch account.
EndToEndTestUtils.sleep(15000); // EndToEndTestUtils.sleep(15000);
EndToEndTestUtils.SOLO.goBack(); // EndToEndTestUtils.SOLO.goBack();
EndToEndTestUtils.SOLO.goBack(); // EndToEndTestUtils.SOLO.goBack();
EndToEndTestUtils.instrumentation.waitForIdleSync(); // EndToEndTestUtils.instrumentation.waitForIdleSync();
} }
/** /**