tt-rss-android/src/org/fox/ttrss/OnlineActivity.java

1455 lines
40 KiB
Java
Raw Normal View History

2012-09-16 09:25:28 +00:00
package org.fox.ttrss;
2012-09-16 16:46:54 +00:00
import java.lang.reflect.Type;
2012-09-16 09:25:28 +00:00
import java.util.HashMap;
2012-09-16 16:46:54 +00:00
import java.util.List;
2012-09-16 09:25:28 +00:00
2012-09-17 12:28:32 +00:00
import org.fox.ttrss.offline.OfflineActivity;
import org.fox.ttrss.offline.OfflineDownloadService;
import org.fox.ttrss.offline.OfflineUploadService;
2012-09-16 09:25:28 +00:00
import org.fox.ttrss.types.Article;
import org.fox.ttrss.types.ArticleList;
import org.fox.ttrss.types.Feed;
2012-09-16 16:46:54 +00:00
import org.fox.ttrss.types.Label;
2012-09-16 09:25:28 +00:00
2012-09-19 09:37:12 +00:00
import android.annotation.TargetApi;
2012-09-16 09:25:28 +00:00
import android.app.AlertDialog;
import android.app.Dialog;
2012-09-19 18:40:27 +00:00
import android.content.ActivityNotFoundException;
2012-09-17 12:28:32 +00:00
import android.content.BroadcastReceiver;
2012-09-16 09:25:28 +00:00
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
2012-09-16 09:25:28 +00:00
import android.content.Intent;
2012-09-17 12:28:32 +00:00
import android.content.IntentFilter;
2012-09-16 09:25:28 +00:00
import android.content.SharedPreferences;
2012-09-19 18:40:27 +00:00
import android.content.pm.PackageInfo;
2012-12-27 09:06:53 +00:00
import android.content.pm.PackageManager;
2012-09-17 12:28:32 +00:00
import android.database.Cursor;
2012-09-19 18:40:27 +00:00
import android.net.Uri;
2012-09-16 09:25:28 +00:00
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.ActionMode;
import android.view.KeyEvent;
2012-09-16 09:25:28 +00:00
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
2012-09-16 15:25:47 +00:00
import android.widget.EditText;
2012-09-16 09:25:28 +00:00
import android.widget.SearchView;
2012-09-16 17:00:09 +00:00
import android.widget.ShareActionProvider;
2012-09-16 09:25:28 +00:00
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
2012-09-16 09:25:28 +00:00
public class OnlineActivity extends CommonActivity {
private final String TAG = this.getClass().getSimpleName();
2012-12-26 20:01:39 +00:00
private final static int TRIAL_DAYS = 8;
2012-09-16 09:25:28 +00:00
protected SharedPreferences m_prefs;
protected Menu m_menu;
2012-09-16 15:25:47 +00:00
2012-09-17 12:28:32 +00:00
protected int m_offlineModeStatus = 0;
2012-09-16 09:25:28 +00:00
private ActionMode m_headlinesActionMode;
private HeadlinesActionModeCallback m_headlinesActionModeCallback;
2012-09-17 12:28:32 +00:00
private BroadcastReceiver m_broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context content, Intent intent) {
if (intent.getAction().equals(OfflineDownloadService.INTENT_ACTION_SUCCESS)) {
m_offlineModeStatus = 2;
switchOffline();
} else if (intent.getAction().equals(OfflineUploadService.INTENT_ACTION_SUCCESS)) {
Log.d(TAG, "offline upload service reports success");
toast(R.string.offline_sync_success);
}
}
};
2012-09-19 09:37:12 +00:00
@TargetApi(11)
private class HeadlinesActionModeCallback implements ActionMode.Callback {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);
if (hf != null) {
ArticleList selected = hf.getSelectedArticles();
if (selected.size() > 0) {
selected.clear();
initMenu();
hf.notifyUpdated();
}
}
m_headlinesActionMode = null;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.headlines_action_menu, menu);
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
onOptionsItemSelected(item);
return false;
}
};
2012-09-19 09:55:01 +00:00
protected String getSessionId() {
return GlobalState.getInstance().m_sessionId;
}
protected void setSessionId(String sessionId) {
GlobalState.getInstance().m_sessionId = sessionId;
}
2012-09-16 09:25:28 +00:00
@Override
public void onCreate(Bundle savedInstanceState) {
m_prefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
ApiRequest.disableConnectionReuseIfNecessary();
2012-09-16 09:25:28 +00:00
if (m_prefs.getString("theme", "THEME_DARK").equals("THEME_DARK")) {
setTheme(R.style.DarkTheme);
2012-12-30 23:14:01 +00:00
} else if (m_prefs.getString("theme", "THEME_DARK").equals("THEME_SEPIA")) {
setTheme(R.style.SepiaTheme);
2012-09-16 09:25:28 +00:00
} else {
setTheme(R.style.LightTheme);
}
super.onCreate(savedInstanceState);
if (canUseProgress()) {
requestWindowFeature(Window.FEATURE_PROGRESS);
} else {
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
}
2012-09-19 12:01:31 +00:00
setProgressBarVisibility(false);
2012-09-16 09:25:28 +00:00
setProgressBarIndeterminateVisibility(false);
2012-09-17 12:28:32 +00:00
// SharedPreferences localPrefs = getSharedPreferences("localprefs", Context.MODE_PRIVATE);
SharedPreferences localPrefs = getSharedPreferences("localprefs", Context.MODE_PRIVATE);
2012-09-16 09:25:28 +00:00
2012-09-17 12:28:32 +00:00
boolean isOffline = localPrefs.getBoolean("offline_mode_active", false);
Log.d(TAG, "m_isOffline=" + isOffline);
2012-09-17 14:44:43 +00:00
setContentView(R.layout.login);
2012-09-17 12:28:32 +00:00
if (isOffline) {
switchOfflineSuccess();
} else {
2012-12-26 20:01:39 +00:00
checkTrial(false);
2012-09-17 12:28:32 +00:00
2012-09-19 09:55:01 +00:00
/* if (getIntent().getExtras() != null) {
2012-09-17 12:28:32 +00:00
Intent i = getIntent();
2012-09-19 09:55:01 +00:00
} */
2012-09-17 12:28:32 +00:00
if (savedInstanceState != null) {
m_offlineModeStatus = savedInstanceState.getInt("offlineModeStatus");
}
if (!isCompatMode()) {
m_headlinesActionModeCallback = new HeadlinesActionModeCallback();
}
2012-09-16 09:25:28 +00:00
}
2012-09-17 12:28:32 +00:00
}
protected boolean canUseProgress() {
return GlobalState.getInstance().m_canUseProgress;
}
2012-09-17 12:28:32 +00:00
private void switchOffline() {
if (m_offlineModeStatus == 2) {
AlertDialog.Builder builder = new AlertDialog.Builder(
OnlineActivity.this)
.setMessage(R.string.dialog_offline_success)
.setPositiveButton(R.string.dialog_offline_go,
new Dialog.OnClickListener() {
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);
editor.commit();
Intent offline = new Intent(
OnlineActivity.this,
OfflineActivity.class);
offline.putExtra("initial", true);
startActivity(offline);
finish();
}
})
.setNegativeButton(R.string.dialog_cancel,
new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
m_offlineModeStatus = 0;
}
});
AlertDialog dlg = builder.create();
dlg.show();
} else if (m_offlineModeStatus == 0) {
2012-09-16 09:25:28 +00:00
2012-09-17 12:28:32 +00:00
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setMessage(R.string.dialog_offline_switch_prompt)
.setPositiveButton(R.string.dialog_offline_go,
new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
2012-09-19 09:55:01 +00:00
if (getSessionId() != null) {
2012-09-17 12:28:32 +00:00
Log.d(TAG, "offline: starting");
m_offlineModeStatus = 1;
Intent intent = new Intent(
OnlineActivity.this,
OfflineDownloadService.class);
2012-09-19 09:55:01 +00:00
intent.putExtra("sessionId", getSessionId());
2012-09-17 12:28:32 +00:00
startService(intent);
}
}
})
.setNegativeButton(R.string.dialog_cancel,
new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
//
}
});
AlertDialog dlg = builder.create();
dlg.show();
} else if (m_offlineModeStatus == 1) {
cancelOfflineSync();
}
2012-09-17 12:28:32 +00:00
}
private boolean hasPendingOfflineData() {
try {
Cursor c = getReadableDb().query("articles",
new String[] { "COUNT(*)" }, "modified = 1", null, null, null,
null);
if (c.moveToFirst()) {
int modified = c.getInt(0);
c.close();
return modified > 0;
}
} catch (IllegalStateException e) {
// db is closed? ugh
}
return false;
}
private boolean hasOfflineData() {
try {
Cursor c = getReadableDb().query("articles",
new String[] { "COUNT(*)" }, null, null, null, null, null);
if (c.moveToFirst()) {
int modified = c.getInt(0);
c.close();
return modified > 0;
}
} catch (IllegalStateException e) {
// db is closed?
}
return false;
}
@Override
public void onPause() {
super.onPause();
unregisterReceiver(m_broadcastReceiver);
}
@Override
public void onDestroy() {
super.onDestroy();
}
private void syncOfflineData() {
Log.d(TAG, "offlineSync: starting");
2012-09-17 12:28:32 +00:00
Intent intent = new Intent(
OnlineActivity.this,
OfflineUploadService.class);
2012-09-16 09:25:28 +00:00
2012-09-19 09:55:01 +00:00
intent.putExtra("sessionId", getSessionId());
2012-09-17 12:28:32 +00:00
startService(intent);
}
private void cancelOfflineSync() {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setMessage(R.string.dialog_offline_sync_in_progress)
.setNegativeButton(R.string.dialog_offline_sync_stop,
new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
2012-09-19 09:55:01 +00:00
if (getSessionId() != null) {
2012-09-17 12:28:32 +00:00
Log.d(TAG, "offline: stopping");
m_offlineModeStatus = 0;
Intent intent = new Intent(
OnlineActivity.this,
OfflineDownloadService.class);
stopService(intent);
dialog.dismiss();
restart();
}
}
})
.setPositiveButton(R.string.dialog_offline_sync_continue,
new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
restart();
}
});
AlertDialog dlg = builder.create();
dlg.show();
}
public void restart() {
Intent refresh = new Intent(OnlineActivity.this, OnlineActivity.class);
startActivity(refresh);
finish();
2012-09-16 09:25:28 +00:00
}
2012-09-17 12:28:32 +00:00
private void switchOfflineSuccess() {
logout();
// setLoadingStatus(R.string.blank, false);
SharedPreferences.Editor editor = m_prefs.edit();
editor.putBoolean("offline_mode_active", true);
editor.commit();
Intent offline = new Intent(OnlineActivity.this, OfflineActivity.class);
offline.putExtra("initial", true);
offline.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivityForResult(offline, 0);
finish();
2012-09-16 09:25:28 +00:00
2012-09-17 12:28:32 +00:00
}
public void login() {
login(false);
}
public void login(boolean refresh) {
2012-09-16 09:25:28 +00:00
if (m_prefs.getString("ttrss_url", "").trim().length() == 0) {
setLoadingStatus(R.string.login_need_configure, false);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.dialog_need_configure_prompt)
.setCancelable(false)
.setPositiveButton(R.string.dialog_open_preferences, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// launch preferences
Intent intent = new Intent(OnlineActivity.this,
PreferencesActivity.class);
startActivityForResult(intent, 0);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
} else {
2012-09-21 06:56:49 +00:00
setLoadingStatus(R.string.login_in_progress, true);
LoginRequest ar = new LoginRequest(getApplicationContext(), refresh);
2012-09-16 09:25:28 +00:00
HashMap<String, String> map = new HashMap<String, String>() {
{
put("op", "login");
put("user", m_prefs.getString("login", "").trim());
put("password", m_prefs.getString("password", "").trim());
}
};
ar.execute(map);
setLoadingStatus(R.string.login_in_progress, true);
}
}
protected void loginSuccess(boolean refresh) {
2012-09-16 09:25:28 +00:00
setLoadingStatus(R.string.blank, false);
initMenu();
Intent intent = new Intent(OnlineActivity.this, FeedsActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivityForResult(intent, 0);
2012-12-30 21:53:04 +00:00
overridePendingTransition(0, 0);
2012-09-17 12:28:32 +00:00
if (hasPendingOfflineData())
syncOfflineData();
2012-09-16 09:25:28 +00:00
2012-12-26 20:01:39 +00:00
finish();
}
public void checkTrial(boolean notify) {
2012-12-27 09:06:53 +00:00
boolean isTrial = getPackageManager().checkSignatures(
getPackageName(), "org.fox.ttrss.key") != PackageManager.SIGNATURE_MATCH;
2012-12-26 20:01:39 +00:00
if (isTrial) {
long firstStart = m_prefs.getLong("date_firstlaunch_trial", -1);
if (firstStart == -1) {
firstStart = System.currentTimeMillis();
SharedPreferences.Editor editor = m_prefs.edit();
editor.putLong("date_firstlaunch_trial", firstStart);
editor.commit();
}
if (!notify && System.currentTimeMillis() > firstStart + (TRIAL_DAYS * 24 * 60 * 60 * 1000)) {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle(R.string.trial_expired)
.setMessage(R.string.trial_expired_message)
.setCancelable(false)
.setPositiveButton(getString(R.string.trial_purchase),
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
openUnlockUrl();
finish();
}
})
.setNegativeButton(getString(R.string.cancel),
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
finish();
}
});
2012-12-26 20:01:39 +00:00
AlertDialog dialog = builder.create();
dialog.show();
} else {
long daysLeft = Math.round((firstStart + (TRIAL_DAYS * 24 * 60 * 60 * 1000) - System.currentTimeMillis()) / (24 * 60 * 60 * 1000));
if (notify) {
toast(getString(R.string.trial_mode_prompt, Long.valueOf(daysLeft)));
}
}
} else if (notify) {
//toast(R.string.trial_thanks);
2012-12-26 20:01:39 +00:00
}
}
private void openUnlockUrl() {
try {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=org.fox.ttrss.key"));
startActivity(intent);
} catch (ActivityNotFoundException ae) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=org.fox.ttrss.key"));
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
toast(R.string.error_other_error);
}
}
2012-09-16 09:25:28 +00:00
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
2012-09-16 15:25:47 +00:00
final HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);
2012-09-16 16:46:54 +00:00
final ArticlePager ap = (ArticlePager)getSupportFragmentManager().findFragmentByTag(FRAG_ARTICLE);
2012-09-16 09:25:28 +00:00
switch (item.getItemId()) {
2012-09-17 19:35:52 +00:00
case android.R.id.home:
finish();
return true;
case R.id.toggle_attachments:
if (true) {
Article article = ap.getSelectedArticle();
if (article != null && article.attachments != null && article.attachments.size() > 0) {
CharSequence[] items = new CharSequence[article.attachments.size()];
final CharSequence[] itemUrls = new CharSequence[article.attachments.size()];
for (int i = 0; i < article.attachments.size(); i++) {
items[i] = article.attachments.get(i).title != null ? article.attachments.get(i).content_url :
article.attachments.get(i).content_url;
itemUrls[i] = article.attachments.get(i).content_url;
}
Dialog dialog = new Dialog(OnlineActivity.this);
AlertDialog.Builder builder = new AlertDialog.Builder(OnlineActivity.this)
.setTitle(R.string.attachments_prompt)
.setSingleChoiceItems(items, 0, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//
}
}).setNeutralButton(R.string.attachment_copy, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
copyToClipboard((String)itemUrls[selectedPosition]);
}
}).setPositiveButton(R.string.attachment_view, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse((String)itemUrls[selectedPosition]));
startActivity(browserIntent);
dialog.cancel();
}
}).setNegativeButton(R.string.dialog_cancel, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
dialog = builder.create();
dialog.show();
}
}
return true;
2012-09-19 18:40:27 +00:00
case R.id.donate:
if (true) {
2012-12-26 20:01:39 +00:00
openUnlockUrl();
2012-09-19 18:40:27 +00:00
}
return true;
2012-09-16 09:25:28 +00:00
case R.id.logout:
logout();
return true;
case R.id.login:
login();
return true;
2012-09-16 15:25:47 +00:00
case R.id.go_offline:
2012-09-17 12:28:32 +00:00
switchOffline();
2012-09-16 15:25:47 +00:00
return true;
2012-09-16 16:46:54 +00:00
case R.id.article_set_note:
if (ap != null && ap.getSelectedArticle() != null) {
editArticleNote(ap.getSelectedArticle());
}
return true;
2012-09-16 09:25:28 +00:00
case R.id.preferences:
Intent intent = new Intent(OnlineActivity.this,
PreferencesActivity.class);
startActivityForResult(intent, 0);
return true;
2012-09-16 15:25:47 +00:00
case R.id.search:
if (hf != null && isCompatMode()) {
Dialog dialog = new Dialog(this);
final EditText edit = new EditText(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle(R.string.search)
.setPositiveButton(getString(R.string.search),
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
String query = edit.getText().toString().trim();
hf.setSearchQuery(query);
}
})
.setNegativeButton(getString(R.string.cancel),
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
//
}
}).setView(edit);
dialog = builder.create();
dialog.show();
}
return true;
case R.id.headlines_mark_as_read:
if (hf != null) {
ArticleList articles = hf.getUnreadArticles();
for (Article a : articles)
a.unread = false;
ApiRequest req = new ApiRequest(getApplicationContext()) {
protected void onPostExecute(JsonElement result) {
if (hf.isAdded()) {
hf.refresh(false);
}
2012-09-16 15:25:47 +00:00
}
};
final String articleIds = articlesToIdString(articles);
@SuppressWarnings("serial")
HashMap<String, String> map = new HashMap<String, String>() {
{
2012-09-19 09:55:01 +00:00
put("sid", getSessionId());
2012-09-16 15:25:47 +00:00
put("op", "updateArticle");
put("article_ids", articleIds);
put("mode", "0");
put("field", "2");
}
};
req.execute(map);
}
return true;
case R.id.headlines_select:
if (hf != null) {
Dialog dialog = new Dialog(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle(R.string.headlines_select_dialog)
.setSingleChoiceItems(
new String[] {
getString(R.string.headlines_select_all),
getString(R.string.headlines_select_unread),
getString(R.string.headlines_select_none) },
0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
switch (which) {
case 0:
hf.setSelection(HeadlinesFragment.ArticlesSelection.ALL);
break;
case 1:
hf.setSelection(HeadlinesFragment.ArticlesSelection.UNREAD);
break;
case 2:
hf.setSelection(HeadlinesFragment.ArticlesSelection.NONE);
break;
}
dialog.cancel();
initMenu();
}
});
dialog = builder.create();
dialog.show();
}
return true;
2012-09-16 16:46:54 +00:00
case R.id.share_article:
if (android.os.Build.VERSION.SDK_INT < 14) {
if (ap != null) {
shareArticle(ap.getSelectedArticle());
}
}
return true;
case R.id.toggle_marked:
if (ap != null & ap.getSelectedArticle() != null) {
Article a = ap.getSelectedArticle();
a.marked = !a.marked;
saveArticleMarked(a);
if (hf != null) hf.notifyUpdated();
}
return true;
case R.id.selection_select_none:
if (hf != null) {
ArticleList selected = hf.getSelectedArticles();
if (selected.size() > 0) {
selected.clear();
initMenu();
hf.notifyUpdated();
}
}
return true;
case R.id.selection_toggle_unread:
if (hf != null) {
ArticleList selected = hf.getSelectedArticles();
if (selected.size() > 0) {
for (Article a : selected)
a.unread = !a.unread;
toggleArticlesUnread(selected);
hf.notifyUpdated();
}
}
return true;
case R.id.selection_toggle_marked:
if (hf != null) {
ArticleList selected = hf.getSelectedArticles();
if (selected.size() > 0) {
for (Article a : selected)
a.marked = !a.marked;
toggleArticlesMarked(selected);
hf.notifyUpdated();
}
}
return true;
case R.id.selection_toggle_published:
if (hf != null) {
ArticleList selected = hf.getSelectedArticles();
if (selected.size() > 0) {
for (Article a : selected)
a.published = !a.published;
toggleArticlesPublished(selected);
hf.notifyUpdated();
}
}
return true;
case R.id.toggle_published:
if (ap != null && ap.getSelectedArticle() != null) {
Article a = ap.getSelectedArticle();
a.published = !a.published;
saveArticlePublished(a);
if (hf != null) hf.notifyUpdated();
}
return true;
case R.id.catchup_above:
if (hf != null) {
if (ap != null && ap.getSelectedArticle() != null) {
Article article = ap.getSelectedArticle();
ArticleList articles = hf.getAllArticles();
ArticleList tmp = new ArticleList();
for (Article a : articles) {
a.unread = false;
tmp.add(a);
if (article.id == a.id)
break;
}
if (tmp.size() > 0) {
toggleArticlesUnread(tmp);
hf.notifyUpdated();
}
}
}
return true;
case R.id.set_unread:
if (ap != null && ap.getSelectedArticle() != null) {
Article a = ap.getSelectedArticle();
a.unread = true;
saveArticleUnread(a);
if (hf != null) hf.notifyUpdated();
}
return true;
case R.id.set_labels:
if (ap != null && ap.getSelectedArticle() != null) {
editArticleLabels(ap.getSelectedArticle());
}
return true;
case R.id.update_headlines:
if (hf != null) {
hf.refresh(false);
}
return true;
2012-09-16 09:25:28 +00:00
default:
Log.d(TAG, "onOptionsItemSelected, unhandled id=" + item.getItemId());
return super.onOptionsItemSelected(item);
}
}
2012-09-16 16:46:54 +00:00
public void editArticleNote(final Article article) {
String note = "";
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(article.title);
final EditText topicEdit = new EditText(this);
topicEdit.setText(note);
builder.setView(topicEdit);
builder.setPositiveButton(R.string.article_set_note, new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
saveArticleNote(article, topicEdit.getText().toString().trim());
article.published = true;
saveArticlePublished(article);
HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);
if (hf != null) hf.notifyUpdated();
}
});
builder.setNegativeButton(R.string.dialog_cancel, new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
public void editArticleLabels(Article article) {
final int articleId = article.id;
ApiRequest req = new ApiRequest(getApplicationContext()) {
@Override
protected void onPostExecute(JsonElement result) {
if (result != null) {
Type listType = new TypeToken<List<Label>>() {}.getType();
final List<Label> labels = new Gson().fromJson(result, listType);
CharSequence[] items = new CharSequence[labels.size()];
final int[] itemIds = new int[labels.size()];
boolean[] checkedItems = new boolean[labels.size()];
for (int i = 0; i < labels.size(); i++) {
items[i] = labels.get(i).caption;
itemIds[i] = labels.get(i).id;
checkedItems[i] = labels.get(i).checked;
}
Dialog dialog = new Dialog(OnlineActivity.this);
AlertDialog.Builder builder = new AlertDialog.Builder(OnlineActivity.this)
.setTitle(R.string.article_set_labels)
.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, final boolean isChecked) {
final int labelId = itemIds[which];
@SuppressWarnings("serial")
HashMap<String, String> map = new HashMap<String, String>() {
{
2012-09-19 09:55:01 +00:00
put("sid", getSessionId());
2012-09-16 16:46:54 +00:00
put("op", "setArticleLabel");
put("label_id", String.valueOf(labelId));
put("article_ids", String.valueOf(articleId));
if (isChecked) put("assign", "true");
}
};
ApiRequest req = new ApiRequest(m_context);
req.execute(map);
}
}).setPositiveButton(R.string.dialog_close, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialog = builder.create();
dialog.show();
}
}
};
@SuppressWarnings("serial")
HashMap<String, String> map = new HashMap<String, String>() {
{
2012-09-19 09:55:01 +00:00
put("sid", getSessionId());
2012-09-16 16:46:54 +00:00
put("op", "getLabels");
put("article_id", String.valueOf(articleId));
}
};
req.execute(map);
}
2012-09-16 09:25:28 +00:00
protected void logout() {
2012-09-19 09:55:01 +00:00
setSessionId(null);
2012-09-16 09:25:28 +00:00
findViewById(R.id.loading_container).setVisibility(View.VISIBLE);
setLoadingStatus(R.string.login_ready, false);
initMenu();
}
protected void loginFailure() {
2012-09-19 09:55:01 +00:00
setSessionId(null);
2012-09-16 09:25:28 +00:00
initMenu();
2012-09-17 12:28:32 +00:00
if (hasOfflineData()) {
AlertDialog.Builder builder = new AlertDialog.Builder(
OnlineActivity.this)
.setMessage(R.string.dialog_offline_prompt)
.setPositiveButton(R.string.dialog_offline_go,
new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
switchOfflineSuccess();
}
})
.setNegativeButton(R.string.dialog_cancel,
new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
//
}
});
AlertDialog dlg = builder.create();
dlg.show();
}
2012-09-16 09:25:28 +00:00
}
2012-09-16 15:25:47 +00:00
2012-09-16 09:25:28 +00:00
@Override
public void onSaveInstanceState(Bundle out) {
super.onSaveInstanceState(out);
2012-09-17 12:28:32 +00:00
out.putInt("offlineModeStatus", m_offlineModeStatus);
2012-09-16 09:25:28 +00:00
}
@Override
public void onResume() {
super.onResume();
ApiRequest.trustAllHosts(m_prefs.getBoolean("ssl_trust_any", false),
m_prefs.getBoolean("ssl_trust_any_host", false));
2012-09-17 12:28:32 +00:00
IntentFilter filter = new IntentFilter();
filter.addAction(OfflineDownloadService.INTENT_ACTION_SUCCESS);
filter.addAction(OfflineUploadService.INTENT_ACTION_SUCCESS);
filter.addCategory(Intent.CATEGORY_DEFAULT);
registerReceiver(m_broadcastReceiver, filter);
2012-09-19 09:55:01 +00:00
if (getSessionId() == null) {
2012-09-16 09:25:28 +00:00
login();
} else {
loginSuccess(false);
2012-09-16 09:25:28 +00:00
}
}
public Menu getMenu() {
return m_menu;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
m_menu = menu;
initMenu();
2012-09-19 18:40:27 +00:00
List<PackageInfo> pkgs = getPackageManager()
.getInstalledPackages(0);
for (PackageInfo p : pkgs) {
if ("org.fox.ttrss.key".equals(p.packageName)) {
Log.d(TAG, "license apk found");
menu.findItem(R.id.donate).setVisible(false);
break;
}
}
2012-09-16 09:25:28 +00:00
return true;
}
2012-09-19 09:55:01 +00:00
protected int getApiLevel() {
return GlobalState.getInstance().m_apiLevel;
2012-09-16 09:25:28 +00:00
}
2012-09-19 09:55:01 +00:00
protected void setApiLevel(int apiLevel) {
GlobalState.getInstance().m_apiLevel = apiLevel;
2012-09-16 09:25:28 +00:00
}
@SuppressWarnings({ "unchecked", "serial" })
public void saveArticleUnread(final Article article) {
ApiRequest req = new ApiRequest(getApplicationContext());
HashMap<String, String> map = new HashMap<String, String>() {
{
2012-09-19 09:55:01 +00:00
put("sid", getSessionId());
2012-09-16 09:25:28 +00:00
put("op", "updateArticle");
put("article_ids", String.valueOf(article.id));
put("mode", article.unread ? "1" : "0");
put("field", "2");
}
};
req.execute(map);
}
@SuppressWarnings({ "unchecked", "serial" })
public void saveArticleMarked(final Article article) {
ApiRequest req = new ApiRequest(getApplicationContext()) {
protected void onPostExecute(JsonElement result) {
toast(article.marked ? R.string.notify_article_marked : R.string.notify_article_unmarked);
}
};
HashMap<String, String> map = new HashMap<String, String>() {
{
2012-09-19 09:55:01 +00:00
put("sid", getSessionId());
2012-09-16 09:25:28 +00:00
put("op", "updateArticle");
put("article_ids", String.valueOf(article.id));
put("mode", article.marked ? "1" : "0");
put("field", "0");
}
};
req.execute(map);
}
@SuppressWarnings({ "unchecked", "serial" })
public void saveArticlePublished(final Article article) {
ApiRequest req = new ApiRequest(getApplicationContext()) {
protected void onPostExecute(JsonElement result) {
toast(article.published ? R.string.notify_article_published : R.string.notify_article_unpublished);
}
};
HashMap<String, String> map = new HashMap<String, String>() {
{
2012-09-19 09:55:01 +00:00
put("sid", getSessionId());
2012-09-16 09:25:28 +00:00
put("op", "updateArticle");
put("article_ids", String.valueOf(article.id));
put("mode", article.published ? "1" : "0");
put("field", "1");
}
};
req.execute(map);
}
@SuppressWarnings({ "unchecked", "serial" })
public void saveArticleNote(final Article article, final String note) {
ApiRequest req = new ApiRequest(getApplicationContext()) {
protected void onPostExecute(JsonElement result) {
toast(R.string.notify_article_note_set);
}
};
HashMap<String, String> map = new HashMap<String, String>() {
{
2012-09-19 09:55:01 +00:00
put("sid", getSessionId());
2012-09-16 09:25:28 +00:00
put("op", "updateArticle");
put("article_ids", String.valueOf(article.id));
put("mode", "1");
put("data", note);
put("field", "3");
}
};
req.execute(map);
}
public static String articlesToIdString(ArticleList articles) {
String tmp = "";
for (Article a : articles)
tmp += String.valueOf(a.id) + ",";
return tmp.replaceAll(",$", "");
}
2012-09-19 05:45:03 +00:00
public void shareText(String text) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(intent, text));
}
2012-09-16 09:25:28 +00:00
public void shareArticle(Article article) {
if (article != null) {
Intent intent = getShareIntent(article);
startActivity(Intent.createChooser(intent,
getString(R.string.share_article)));
}
}
2012-09-16 16:46:54 +00:00
protected Intent getShareIntent(Article article) {
2012-09-16 09:25:28 +00:00
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, article.title);
intent.putExtra(Intent.EXTRA_TEXT, article.link);
return intent;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (m_prefs.getBoolean("use_volume_keys", false)) {
ArticlePager ap = (ArticlePager) getSupportFragmentManager().findFragmentByTag(FRAG_ARTICLE);
if (ap != null && ap.isAdded()) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
ap.selectArticle(false);
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
ap.selectArticle(true);
return true;
}
}
}
return super.onKeyDown(keyCode, event);
}
2012-09-16 09:25:28 +00:00
@SuppressWarnings("unchecked")
public void catchupFeed(final Feed feed) {
Log.d(TAG, "catchupFeed=" + feed);
ApiRequest req = new ApiRequest(getApplicationContext()) {
protected void onPostExecute(JsonElement result) {
// refresh?
}
};
@SuppressWarnings("serial")
HashMap<String, String> map = new HashMap<String, String>() {
{
2012-09-19 09:55:01 +00:00
put("sid", getSessionId());
2012-09-16 09:25:28 +00:00
put("op", "catchupFeed");
put("feed_id", String.valueOf(feed.id));
if (feed.is_cat)
put("is_cat", "1");
}
};
req.execute(map);
}
@SuppressWarnings("unchecked")
public void toggleArticlesMarked(final ArticleList articles) {
ApiRequest req = new ApiRequest(getApplicationContext());
@SuppressWarnings("serial")
HashMap<String, String> map = new HashMap<String, String>() {
{
2012-09-19 09:55:01 +00:00
put("sid", getSessionId());
2012-09-16 09:25:28 +00:00
put("op", "updateArticle");
put("article_ids", articlesToIdString(articles));
put("mode", "2");
put("field", "0");
}
};
req.execute(map);
}
@SuppressWarnings("unchecked")
public void toggleArticlesUnread(final ArticleList articles) {
ApiRequest req = new ApiRequest(getApplicationContext());
@SuppressWarnings("serial")
HashMap<String, String> map = new HashMap<String, String>() {
{
2012-09-19 09:55:01 +00:00
put("sid", getSessionId());
2012-09-16 09:25:28 +00:00
put("op", "updateArticle");
put("article_ids", articlesToIdString(articles));
put("mode", "2");
put("field", "2");
}
};
req.execute(map);
//refresh();
}
@SuppressWarnings("unchecked")
public void toggleArticlesPublished(final ArticleList articles) {
ApiRequest req = new ApiRequest(getApplicationContext());
@SuppressWarnings("serial")
HashMap<String, String> map = new HashMap<String, String>() {
{
2012-09-19 09:55:01 +00:00
put("sid", getSessionId());
2012-09-16 09:25:28 +00:00
put("op", "updateArticle");
put("article_ids", articlesToIdString(articles));
put("mode", "2");
put("field", "1");
}
};
req.execute(map);
}
protected void initMenu() {
if (m_menu != null) {
2012-09-19 09:55:01 +00:00
if (getSessionId() != null) {
2012-09-16 09:25:28 +00:00
m_menu.setGroupVisible(R.id.menu_group_logged_in, true);
m_menu.setGroupVisible(R.id.menu_group_logged_out, false);
} else {
m_menu.setGroupVisible(R.id.menu_group_logged_in, false);
m_menu.setGroupVisible(R.id.menu_group_logged_out, true);
}
m_menu.setGroupVisible(R.id.menu_group_headlines, false);
m_menu.setGroupVisible(R.id.menu_group_headlines_selection, false);
m_menu.setGroupVisible(R.id.menu_group_article, false);
m_menu.setGroupVisible(R.id.menu_group_feeds, false);
2012-09-19 09:55:01 +00:00
m_menu.findItem(R.id.set_labels).setEnabled(getApiLevel() >= 1);
m_menu.findItem(R.id.article_set_note).setEnabled(getApiLevel() >= 1);
2012-09-16 09:25:28 +00:00
MenuItem search = m_menu.findItem(R.id.search);
2012-09-19 09:55:01 +00:00
search.setEnabled(getApiLevel() >= 2);
2012-09-16 09:25:28 +00:00
2012-09-16 17:00:09 +00:00
if (android.os.Build.VERSION.SDK_INT >= 14) {
ShareActionProvider shareProvider = (ShareActionProvider) m_menu.findItem(R.id.share_article).getActionProvider();
ArticlePager af = (ArticlePager) getSupportFragmentManager().findFragmentByTag(FRAG_ARTICLE);
if (af != null && af.getSelectedArticle() != null) {
shareProvider.setShareIntent(getShareIntent(af.getSelectedArticle()));
if (!isSmallScreen()) {
m_menu.findItem(R.id.share_article).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
}
}
2012-09-16 09:25:28 +00:00
if (!isCompatMode()) {
HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);
if (hf != null) {
if (hf.getSelectedArticles().size() > 0 && m_headlinesActionMode == null) {
m_headlinesActionMode = startActionMode(m_headlinesActionModeCallback);
} else if (hf.getSelectedArticles().size() == 0 && m_headlinesActionMode != null) {
m_headlinesActionMode.finish();
}
}
2012-09-16 09:25:28 +00:00
SearchView searchView = (SearchView) search.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
private String query = "";
@Override
public boolean onQueryTextSubmit(String query) {
HeadlinesFragment frag = (HeadlinesFragment) getSupportFragmentManager()
.findFragmentByTag(FRAG_HEADLINES);
if (frag != null) {
frag.setSearchQuery(query);
this.query = query;
}
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (newText.equals("") && !newText.equals(this.query)) {
HeadlinesFragment frag = (HeadlinesFragment) getSupportFragmentManager()
.findFragmentByTag(FRAG_HEADLINES);
if (frag != null) {
frag.setSearchQuery(newText);
this.query = newText;
}
}
return false;
}
});
}
}
}
2012-09-19 09:37:12 +00:00
protected void refresh(boolean includeHeadlines) {
FeedCategoriesFragment cf = (FeedCategoriesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_CATS);
if (cf != null) {
cf.refresh(false);
}
FeedsFragment ff = (FeedsFragment) getSupportFragmentManager().findFragmentByTag(FRAG_FEEDS);
if (ff != null) {
ff.refresh(false);
}
2012-09-19 09:37:12 +00:00
if (includeHeadlines) {
HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);
2012-09-19 09:37:12 +00:00
if (hf != null) {
hf.refresh(false);
}
ArticlePager af = (ArticlePager) getSupportFragmentManager().findFragmentByTag(FRAG_ARTICLE);
if (af != null) {
af.refresh(false);
}
}
}
2012-09-19 09:37:12 +00:00
protected void refresh() {
refresh(true);
}
2012-09-16 09:25:28 +00:00
private class LoginRequest extends ApiRequest {
boolean m_refreshAfterLogin = false;
public LoginRequest(Context context, boolean refresh) {
2012-09-16 09:25:28 +00:00
super(context);
m_refreshAfterLogin = refresh;
2012-09-16 09:25:28 +00:00
}
@SuppressWarnings("unchecked")
protected void onPostExecute(JsonElement result) {
if (result != null) {
try {
JsonObject content = result.getAsJsonObject();
2012-09-29 07:59:04 +00:00
2012-09-16 09:25:28 +00:00
if (content != null) {
2012-09-19 09:55:01 +00:00
setSessionId(content.get("session_id").getAsString());
2012-09-29 07:59:04 +00:00
JsonElement apiLevel = content.get("api_level");
2012-09-16 09:25:28 +00:00
GlobalState.getInstance().m_canUseProgress = m_canUseProgress;
2012-09-29 07:59:04 +00:00
Log.d(TAG, "Authenticated! canUseProgress=" + m_canUseProgress);
2012-12-26 20:01:39 +00:00
2012-09-29 07:59:04 +00:00
if (apiLevel != null) {
setApiLevel(apiLevel.getAsInt());
Log.d(TAG, "Received API level: " + getApiLevel());
loginSuccess(m_refreshAfterLogin);
} else {
2012-09-16 09:25:28 +00:00
2012-09-29 07:59:04 +00:00
ApiRequest req = new ApiRequest(m_context) {
protected void onPostExecute(JsonElement result) {
setApiLevel(0);
if (result != null) {
try {
setApiLevel(result.getAsJsonObject().get("level").getAsInt());
} catch (Exception e) {
e.printStackTrace();
}
} else if (m_lastError != ApiError.API_UNKNOWN_METHOD) {
// Unknown method means old tt-rss, in that case we assume API 0 and continue
setLoadingStatus(getErrorMessage(), false);
loginFailure();
return;
2012-09-16 09:25:28 +00:00
}
2012-09-29 07:59:04 +00:00
Log.d(TAG, "Received API level: " + getApiLevel());
loginSuccess(m_refreshAfterLogin);
return;
2012-09-16 09:25:28 +00:00
}
2012-09-29 07:59:04 +00:00
};
@SuppressWarnings("serial")
HashMap<String, String> map = new HashMap<String, String>() {
{
put("sid", getSessionId());
put("op", "getApiLevel");
}
};
req.execute(map);
setLoadingStatus(R.string.loading_message, true);
}
2012-09-16 09:25:28 +00:00
return;
}
} catch (Exception e) {
e.printStackTrace();
}
}
2012-09-19 09:55:01 +00:00
setSessionId(null);
2012-09-16 09:25:28 +00:00
setLoadingStatus(getErrorMessage(), false);
loginFailure();
}
}
}