Skip to content

Commit

Permalink
Issue rtyley#100: Add a settings screen with a single setting that co…
Browse files Browse the repository at this point in the history
…ntrols sync frequency.
  • Loading branch information
jwir3 committed Mar 16, 2014
1 parent 685d5bb commit fa7b122
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 5 deletions.
6 changes: 6 additions & 0 deletions agit/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
</intent-filter>
</activity>

<activity android:name="SettingsActivity">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>

<activity android:name=".AboutUsingSshActivity">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
Expand Down
17 changes: 17 additions & 0 deletions agit/res/layout/settings_activity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/settings">

<PreferenceCategory
android:title="@string/settings_category_sync">

<EditTextPreference
android:key="setting_sync_frequency"
android:title="@string/setting_title_sync_frequency"
android:summary="@string/setting_instruction_sync_frequency"
android:inputType="number">
</EditTextPreference>

</PreferenceCategory>

</PreferenceScreen>
5 changes: 5 additions & 0 deletions agit/res/menu/dashboard.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
android:title="@string/open_menu_option"
android:showAsAction="never">
</item>
<item
android:id="@+id/settings"
android:showAsAction="never"
android:title="@string/settings_app_menu_option">
</item>
<item
android:id="@+id/about_app"
android:showAsAction="never"
Expand Down
6 changes: 6 additions & 0 deletions agit/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<string name="using_ssh_activity_title">SSH Install Guide</string>
<string name="clone_launcher_activity_title">Clone...</string>
<string name="clone_menu_option">Clone...</string>
<string name="settings_app_menu_option">Settings...</string>
<string name="enter_clone_url">Url</string>
<string name="clone_target_folder_label">Target Folder</string>
<string name="clone_url_hint">Repository url</string>
Expand Down Expand Up @@ -112,4 +113,9 @@

<string name="no_viewer_available_for_file">"No viewer available for '%1$s'"</string>

<!-- Settings Strings -->
<string name="settings">Settings</string>
<string name="settings_category_sync">Sync Settings</string>
<string name="setting_title_sync_frequency">Sync Frequency (Minutes)</string>
<string name="setting_instruction_sync_frequency">Tap to set how often repository synchronization will occur</string>
</resources>
4 changes: 3 additions & 1 deletion agit/src/main/java/com/madgag/agit/DashboardActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.github.rtyley.android.sherlock.roboguice.activity.RoboSherlockFragmentActivity;
import com.madgag.android.IntentUtil;
import com.madgag.android.util.store.InstallAppDialogFragment;

import java.io.File;
Expand Down Expand Up @@ -81,6 +80,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.clone:
startActivity(new Intent(this, CloneLauncherActivity.class));
return true;
case R.id.settings:
startActivity(new Intent(this, SettingsActivity.class));
return true;
case R.id.open_repo:
if (isIntentAvailable(this, PICK_DIRECTORY_INTENT)) {
Intent intent = new Intent(PICK_DIRECTORY_INTENT);
Expand Down
44 changes: 44 additions & 0 deletions agit/src/main/java/com/madgag/agit/SettingsActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2011, 2012 Roberto Tyley
*
* This file is part of 'Agit' - an Android Git client.
*
* Agit is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Agit is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/ .
*/

package com.madgag.agit;

import static com.madgag.agit.sync.AccountAuthenticatorService.addAccount;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.util.Log;

public class SettingsActivity extends PreferenceActivity {
public void onCreate(Bundle aSavedInstanceState) {
super.onCreate(aSavedInstanceState);
addPreferencesFromResource(R.layout.settings_activity);
}

@Override
public void onDestroy() {
super.onDestroy();
try {
addAccount(this);
} catch (Exception e) {
Log.w(TAG, "Unable to re-add account for syncing after preference changes", e);
}
}

private static final String TAG = "SettingsActivity";
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;

/**
Expand Down Expand Up @@ -71,15 +73,17 @@ public static Bundle addAccount(Context ctx) {
result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
}
configureSyncFor(account);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
int syncFreq = Integer.parseInt(prefs.getString("setting_sync_frequency", "15"));
configureSyncFor(account, syncFreq);
return result;
}

private static void configureSyncFor(Account account) {
Log.d(TAG, "Trying to configure account for sync...");
private static void configureSyncFor(Account account, int syncFreq) {
Log.d(TAG, "Trying to configure account for sync at rate of " + syncFreq + " minutes");
setIsSyncable(account, AGIT_PROVIDER_AUTHORITY, 1);
setSyncAutomatically(account, AGIT_PROVIDER_AUTHORITY, true);
ContentResolver.addPeriodicSync(account, AGIT_PROVIDER_AUTHORITY, new Bundle(), (long) (15 * 60));
ContentResolver.addPeriodicSync(account, AGIT_PROVIDER_AUTHORITY, new Bundle(), (long) (syncFreq * 60));
}

private static class AccountAuthenticatorImpl extends AbstractAccountAuthenticator {
Expand Down
2 changes: 2 additions & 0 deletions agit/src/main/java/com/madgag/agit/sync/SyncAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.content.Context;
import android.content.SyncResult;
import android.os.Bundle;
import android.util.Log;

import com.google.inject.Inject;
import com.google.inject.Singleton;
Expand All @@ -50,6 +51,7 @@ public SyncAdapter(Context context) {
@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider,
SyncResult syncResult) {
Log.d(TAG, "Performing sync");
cancelAnyCurrentCampaign();
contextScope.enter(getContext());
try {
Expand Down

0 comments on commit fa7b122

Please sign in to comment.