From 4fb366178c679a8ef6f8508a722dae4807486c37 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Wed, 20 Jun 2012 09:49:30 +0400 Subject: allow canceling offline download task --- src/org/fox/ttrss/MainActivity.java | 71 ++++++++++++++++++---- .../fox/ttrss/offline/OfflineDownloadService.java | 55 +++++++++++++---- 2 files changed, 100 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/org/fox/ttrss/MainActivity.java b/src/org/fox/ttrss/MainActivity.java index 39eb0ff4..f727910b 100644 --- a/src/org/fox/ttrss/MainActivity.java +++ b/src/org/fox/ttrss/MainActivity.java @@ -49,7 +49,6 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.Window; -import android.view.animation.AnimationUtils; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.EditText; import android.widget.SearchView; @@ -87,7 +86,7 @@ public class MainActivity extends FragmentActivity implements OnlineServices { private int m_apiLevel = 0; private boolean m_isLoggingIn = false; private boolean m_isOffline = false; - private boolean m_offlineModeReady = false; + private int m_offlineModeStatus = 0; private int m_selectedProduct = -1; private SQLiteDatabase m_readableDb; @@ -132,7 +131,7 @@ public class MainActivity extends FragmentActivity implements OnlineServices { if (intent.getAction().equals(OfflineDownloadService.INTENT_ACTION_SUCCESS)) { - m_offlineModeReady = true; + m_offlineModeStatus = 2; switchOffline(); @@ -381,7 +380,7 @@ public class MainActivity extends FragmentActivity implements OnlineServices { } } - private synchronized void refreshHeadlines() { + /* private synchronized void refreshHeadlines() { if (m_sessionId != null) { HeadlinesFragment frag = (HeadlinesFragment) getSupportFragmentManager() .findFragmentByTag(FRAG_HEADLINES); @@ -392,7 +391,7 @@ public class MainActivity extends FragmentActivity implements OnlineServices { frag.refresh(true); } } - } + } */ private synchronized void refreshCategories() { if (m_sessionId != null) { @@ -452,6 +451,12 @@ public class MainActivity extends FragmentActivity implements OnlineServices { } super.onCreate(savedInstanceState); + + if (OfflineDownloadService.INTENT_ACTION_CANCEL.equals(getIntent().getAction())) { + cancelOfflineSync(); + } + + //Log.d(TAG, "started with intent action=" + intentAction); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); @@ -468,7 +473,7 @@ public class MainActivity extends FragmentActivity implements OnlineServices { m_activeCategory = savedInstanceState .getParcelable("activeCategory"); m_apiLevel = savedInstanceState.getInt("apiLevel"); - m_offlineModeReady = savedInstanceState.getBoolean("offlineModeReady"); + m_offlineModeStatus = savedInstanceState.getInt("offlineModeStatus"); } m_enableCats = m_prefs.getBoolean("enable_cats", false); @@ -537,9 +542,8 @@ public class MainActivity extends FragmentActivity implements OnlineServices { return m_writableDb; } - @SuppressWarnings("unchecked") private void switchOffline() { - if (m_offlineModeReady) { + if (m_offlineModeStatus == 2) { AlertDialog.Builder builder = new AlertDialog.Builder( MainActivity.this) @@ -549,6 +553,8 @@ public class MainActivity extends FragmentActivity implements OnlineServices { public void onClick(DialogInterface dialog, int which) { + m_offlineModeStatus = 0; + SharedPreferences localPrefs = getSharedPreferences("localprefs", Context.MODE_PRIVATE); SharedPreferences.Editor editor = localPrefs.edit(); editor.putBoolean("offline_mode_active", true); @@ -565,14 +571,16 @@ public class MainActivity extends FragmentActivity implements OnlineServices { new Dialog.OnClickListener() { public void onClick(DialogInterface dialog, int which) { - // + + m_offlineModeStatus = 0; + } }); AlertDialog dlg = builder.create(); dlg.show(); - } else { + } else if (m_offlineModeStatus == 0) { AlertDialog.Builder builder = new AlertDialog.Builder(this) .setMessage(R.string.dialog_offline_switch_prompt) @@ -583,6 +591,8 @@ public class MainActivity extends FragmentActivity implements OnlineServices { if (m_sessionId != null) { Log.d(TAG, "offline: starting"); + + m_offlineModeStatus = 1; Intent intent = new Intent( MainActivity.this, @@ -603,9 +613,44 @@ public class MainActivity extends FragmentActivity implements OnlineServices { AlertDialog dlg = builder.create(); dlg.show(); + } else if (m_offlineModeStatus == 1) { + cancelOfflineSync(); } } + private void cancelOfflineSync() { + AlertDialog.Builder builder = new AlertDialog.Builder(this) + .setMessage(R.string.dialog_offline_sync_in_progress) + .setPositiveButton(R.string.dialog_offline_sync_stop, + new Dialog.OnClickListener() { + public void onClick(DialogInterface dialog, + int which) { + + if (m_sessionId != null) { + Log.d(TAG, "offline: stopping"); + + m_offlineModeStatus = 0; + + Intent intent = new Intent( + MainActivity.this, + OfflineDownloadService.class); + + stopService(intent); + } + } + }) + .setNegativeButton(R.string.dialog_cancel, + new Dialog.OnClickListener() { + public void onClick(DialogInterface dialog, + int which) { + // + } + }); + + AlertDialog dlg = builder.create(); + dlg.show(); + } + private void switchOfflineSuccess() { logout(); // setLoadingStatus(R.string.blank, false); @@ -641,7 +686,7 @@ public class MainActivity extends FragmentActivity implements OnlineServices { out.putBoolean("unreadArticlesOnly", m_unreadArticlesOnly); out.putParcelable("activeCategory", m_activeCategory); out.putInt("apiLevel", m_apiLevel); - out.putBoolean("offlineModeReady", m_offlineModeReady); + out.putInt("offlineModeStatus", m_offlineModeStatus); } @Override @@ -1601,13 +1646,13 @@ public class MainActivity extends FragmentActivity implements OnlineServices { ft.commit(); } - private Feed getActiveFeed() { + /* private Feed getActiveFeed() { return m_activeFeed; } private FeedCategory getActiveCategory() { return m_activeCategory; - } + } */ private void logout() { if (m_refreshTask != null) { diff --git a/src/org/fox/ttrss/offline/OfflineDownloadService.java b/src/org/fox/ttrss/offline/OfflineDownloadService.java index f9dc0bc1..216ac61d 100644 --- a/src/org/fox/ttrss/offline/OfflineDownloadService.java +++ b/src/org/fox/ttrss/offline/OfflineDownloadService.java @@ -24,13 +24,16 @@ import android.app.IntentService; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; +import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteStatement; +import android.os.Binder; import android.os.Bundle; import android.os.Environment; +import android.os.IBinder; import android.preference.PreferenceManager; import android.provider.BaseColumns; import android.util.Log; @@ -39,14 +42,15 @@ import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.reflect.TypeToken; -public class OfflineDownloadService extends IntentService { +public class OfflineDownloadService extends Service { private final String TAG = this.getClass().getSimpleName(); public static final int NOTIFY_DOWNLOADING = 1; public static final String INTENT_ACTION_SUCCESS = "org.fox.ttrss.intent.action.DownloadComplete"; + public static final String INTENT_ACTION_CANCEL = "org.fox.ttrss.intent.action.Cancel"; - private static final int OFFLINE_SYNC_SEQ = 60; + private static final int OFFLINE_SYNC_SEQ = 40; private static final int OFFLINE_SYNC_MAX = 500; private SQLiteDatabase m_writableDb; @@ -59,11 +63,21 @@ public class OfflineDownloadService extends IntentService { private boolean m_downloadImages = false; private int m_syncMax; private SharedPreferences m_prefs; + private boolean m_canProceed = true; - public OfflineDownloadService() { - super("OfflineDownloadService"); - } + private final IBinder m_binder = new LocalBinder(); + + public class LocalBinder extends Binder { + OfflineDownloadService getService() { + return OfflineDownloadService.this; + } + } + @Override + public IBinder onBind(Intent intent) { + return m_binder; + } + @Override public void onCreate() { super.onCreate(); @@ -81,8 +95,11 @@ public class OfflineDownloadService extends IntentService { Notification notification = new Notification(R.drawable.icon, getString(R.string.notify_downloading_title), System.currentTimeMillis()); + Intent intent = new Intent(this, MainActivity.class); + intent.setAction(INTENT_ACTION_CANCEL); + PendingIntent contentIntent = PendingIntent.getActivity(this, 0, - new Intent(this, MainActivity.class), 0); + intent, 0); notification.flags |= Notification.FLAG_ONGOING_EVENT; notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE; @@ -100,6 +117,8 @@ public class OfflineDownloadService extends IntentService { m_readableDb.close(); m_writableDb.close(); + m_nmgr.cancel(NOTIFY_DOWNLOADING); + // TODO send notification to activity? m_downloadInProgress = false; @@ -209,7 +228,11 @@ public class OfflineDownloadService extends IntentService { getWritableDb().execSQL("DELETE FROM articles;"); - downloadArticles(); + if (m_canProceed) { + downloadArticles(); + } else { + downloadFailed(); + } } catch (Exception e) { e.printStackTrace(); updateNotification(R.string.offline_switch_error); @@ -240,8 +263,10 @@ public class OfflineDownloadService extends IntentService { @Override public void onDestroy() { super.onDestroy(); - m_nmgr.cancel(NOTIFY_DOWNLOADING); + + m_canProceed = false; + Log.d(TAG, "onDestroy"); //m_readableDb.close(); //m_writableDb.close(); @@ -320,12 +345,16 @@ public class OfflineDownloadService extends IntentService { //m_canGetMoreArticles = articles.size() == 30; m_articleOffset += articles.size(); - Log.d(TAG, "offline: received " + articles.size() + " articles"); + Log.d(TAG, "offline: received " + articles.size() + " articles; canProc=" + m_canProceed); - if (articles.size() == OFFLINE_SYNC_SEQ && m_articleOffset < m_syncMax) { - downloadArticles(); + if (m_canProceed) { + if (articles.size() == OFFLINE_SYNC_SEQ && m_articleOffset < m_syncMax) { + downloadArticles(); + } else { + downloadComplete(); + } } else { - downloadComplete(); + downloadFailed(); } return; @@ -346,7 +375,7 @@ public class OfflineDownloadService extends IntentService { } @Override - protected void onHandleIntent(Intent intent) { + public void onStart(Intent intent, int startId) { m_sessionId = intent.getStringExtra("sessionId"); if (!m_downloadInProgress) { -- cgit v1.2.3