Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix sync bug when restart activity

  • Loading branch information...
commit 21df97daf4e6742840f1aedfdf6202b525d373a9 1 parent c887274
@obiwong authored
View
8 AndroidManifest.xml
@@ -20,6 +20,7 @@
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
+ <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<application android:name="TheApplication"
android:icon="@drawable/icon" android:label="@string/app_name">
@@ -69,8 +70,13 @@
</intent-filter>
</activity>
-
<service android:name=".service.SyncService" />
+
+ <receiver android:name=".receiver.NormalReceiver">
+ <intent-filter>
+ <action android:name="android.intent.action.BOOT_COMPLETED"/>
+ </intent-filter>
+ </receiver>
</application>
</manifest>
View
2  res/values/strings.xml
@@ -103,5 +103,5 @@
<!-- About AndroidV2EX -->
<string name="about_info_title">About AndroidV2EX</string>
- <string name="about_info_summary">version: 0.1</string>
+ <string name="about_info_summary">version: 0.11</string>
</resources>
View
37 src/com/htbest2000/v2ex/receiver/NormalReceiver.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2011 htbest2000@gmail.com
+ *
+ * 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.htbest2000.v2ex.receiver;
+
+import com.htbest2000.v2ex.util.StatFile;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+
+public class NormalReceiver extends BroadcastReceiver {
+ public static final boolean DEBUG = true;
+ public static final String TAG = "=AV2EX=NormalReceiver=";
+
+ @Override
+ public void onReceive(Context ctx, Intent intent) {
+ if (DEBUG) Log.i(TAG, "on boot");
+ if (StatFile.getSyncBush(ctx)) {
+ StatFile.setSyncBusy(ctx, false);
+ }
+ }
+}
View
12 src/com/htbest2000/v2ex/service/SyncService.java
@@ -73,6 +73,14 @@ public void onCreate() {
}
@Override
+ public void onDestroy() {
+ if (StatFile.getSyncBush(this)) {
+ StatFile.setSyncBusy(this, false);
+ }
+ super.onDestroy();
+ }
+
+ @Override
protected void onHandleIntent(Intent intent) {
final String path = intent.getStringExtra(PATH);
final int operator = intent.getIntExtra( OPERATOR, -1);
@@ -111,14 +119,18 @@ protected void onHandleIntent(Intent intent) {
private void fetchTopicList(String path) throws IOException {
mDownloader.setCommand( commandFactory(COMMAND_FETCH_TOPIC_LIST) );
if (DEBUG) Log.d(TAG, "start fetchTopicList");
+ StatFile.setSyncBusy(this, true);
mDownloader.fetchHtml( path );
+ StatFile.setSyncBusy(this, false);
if (DEBUG) Log.d(TAG, "end fetchTopicList");
}
private void fetchNodes(String path) throws IOException {
mDownloader.setCommand( commandFactory(COMMAND_FETCH_TOPIC_NODES) );
if (DEBUG) Log.d(TAG, "start fetchNodes");
+ StatFile.setSyncBusy(this, true);
mDownloader.fetchHtml(path);
+ StatFile.setSyncBusy(this, false);
if (DEBUG) Log.d(TAG, "end fetchNodes");
}
View
30 src/com/htbest2000/v2ex/ui/TopicListActivity.java
@@ -23,6 +23,7 @@
import com.htbest2000.v2ex.service.SyncService;
import com.htbest2000.v2ex.util.DetachableResultReceiver;
import com.htbest2000.v2ex.util.Misc;
+import com.htbest2000.v2ex.util.StatFile;
import com.htbest2000.v2ex.util.ToastMaster;
import com.htbest2000.v2ex.util.State;
@@ -107,6 +108,9 @@ public Object onRetainNonConfigurationInstance() {
protected void onResume() {
super.onResume();
// setupTopicListAdapter();
+ if (StatFile.getSyncBush(this) && !mState.mSyncing) {
+ setRefreshAnimation(true);
+ }
}
@@ -225,22 +229,30 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
+ private void setRefreshAnimation(boolean on) {
+ if (on) {
+ mState.mSyncing = true;
+ mRefreshView.setFocusable(false);
+ mRefreshView.setClickable(false);
+ mRefreshView.startAnimation(mAniSpinSelf);
+ } else {
+ mState.mSyncing = false;
+ mState.mGotError = false;
+ mRefreshView.setFocusable(true);
+ mRefreshView.setClickable(true);
+ mRefreshView.clearAnimation();
+ }
+ }
+
@Override
public void onReceiveResult(int resultCode, Bundle resultData) {
switch (resultCode) {
case SyncService.STATUS_RUNNING: {
- mState.mSyncing = true;
- mRefreshView.setFocusable(false);
- mRefreshView.setClickable(false);
- mRefreshView.startAnimation(mAniSpinSelf);
+ setRefreshAnimation(true);
break;
}
case SyncService.STATUS_FINISHED: {
- mState.mSyncing = false;
- mState.mGotError = false;
- mRefreshView.setFocusable(true);
- mRefreshView.setClickable(true);
- mRefreshView.clearAnimation();
+ setRefreshAnimation(false);
removeOldTopics();
setupTopicListAdapter();
break;
View
1  src/com/htbest2000/v2ex/util/StatFile.java
@@ -24,6 +24,7 @@
private static final String SYNCING = "syncing";
public static void setSyncBusy(Context ctx, boolean busy) {
+
SharedPreferences spref = ctx.getSharedPreferences(FILENAME, Context.MODE_PRIVATE);
spref.edit().putInt(SYNCING, busy?1:0).commit();
}
Please sign in to comment.
Something went wrong with that request. Please try again.