1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
package org.fox.ttrss;
import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Point;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.widget.Toast;
import org.fox.ttrss.util.DatabaseHelper;
public class CommonActivity extends ActionBarActivity {
private final String TAG = this.getClass().getSimpleName();
public final static String FRAG_HEADLINES = "headlines";
public final static String FRAG_ARTICLE = "article";
public final static String FRAG_FEEDS = "feeds";
public final static String FRAG_CATS = "cats";
public final static String THEME_DARK = "THEME_DARK";
public final static String THEME_LIGHT = "THEME_LIGHT";
public final static String THEME_SEPIA = "THEME_SEPIA";
public final static String THEME_AMBER = "THEME_AMBER";
public final static String THEME_DEFAULT = CommonActivity.THEME_LIGHT;
public static final int EXCERPT_MAX_LENGTH = 256;
public static final int EXCERPT_MAX_QUERY_LENGTH = 512;
private SQLiteDatabase m_readableDb;
private SQLiteDatabase m_writableDb;
private boolean m_smallScreenMode = true;
private boolean m_compatMode = false;
private String m_theme;
private boolean m_fullScreen;
protected SharedPreferences m_prefs;
/* protected void enableHttpCaching() {
// enable resource caching
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
try {
File httpCacheDir = new File(getApplicationContext().getCacheDir(), "http");
long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
HttpResponseCache.install(httpCacheDir, httpCacheSize);
} catch (IOException e) {
e.printStackTrace();
}
}
} */
protected void setSmallScreen(boolean smallScreen) {
Log.d(TAG, "m_smallScreenMode=" + smallScreen);
m_smallScreenMode = smallScreen;
}
public boolean getUnreadOnly() {
return m_prefs.getBoolean("show_unread_only", true);
}
// fuck you LG FUCK YOU
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU){
openOptionsMenu();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void setUnreadOnly(boolean unread) {
SharedPreferences.Editor editor = m_prefs.edit();
editor.putBoolean("show_unread_only", unread);
editor.commit();
}
public void toast(int msgId) {
Toast toast = Toast.makeText(CommonActivity.this, msgId, Toast.LENGTH_SHORT);
toast.show();
}
public void toast(String msg) {
Toast toast = Toast.makeText(CommonActivity.this, msg, Toast.LENGTH_SHORT);
toast.show();
}
private void initDatabase() {
DatabaseHelper dh = new DatabaseHelper(getApplicationContext());
m_writableDb = dh.getWritableDatabase();
m_readableDb = dh.getReadableDatabase();
}
public synchronized SQLiteDatabase getReadableDb() {
return m_readableDb;
}
public synchronized SQLiteDatabase getWritableDb() {
return m_writableDb;
}
@Override
public void onResume() {
super.onResume();
if (!m_theme.equals(m_prefs.getString("theme", CommonActivity.THEME_DEFAULT)) ||
m_fullScreen != m_prefs.getBoolean("full_screen_mode", false)) {
Log.d(TAG, "theme changed, restarting");
finish();
startActivity(getIntent());
}
}
@Override
public void onDestroy() {
super.onDestroy();
m_readableDb.close();
m_writableDb.close();
}
@Override
public void onCreate(Bundle savedInstanceState) {
m_prefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
if (savedInstanceState != null) {
m_theme = savedInstanceState.getString("theme");
m_fullScreen = savedInstanceState.getBoolean("fullscreen");
} else {
m_theme = m_prefs.getString("theme", CommonActivity.THEME_DEFAULT);
m_fullScreen = m_prefs.getBoolean("full_screen_mode", false);
}
initDatabase();
m_compatMode = android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB;
Log.d(TAG, "m_compatMode=" + m_compatMode);
super.onCreate(savedInstanceState);
}
public int dpToPx(int dp) {
float density = getResources().getDisplayMetrics().density;
return Math.round((float)dp * density);
}
@Override
public void onSaveInstanceState(Bundle out) {
super.onSaveInstanceState(out);
out.putString("theme", m_theme);
out.putBoolean("fullscreen", m_fullScreen);
}
public boolean isSmallScreen() {
return m_smallScreenMode;
}
public boolean isCompatMode() {
return m_compatMode;
}
@SuppressWarnings("deprecation")
public boolean isPortrait() {
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
return width < height;
}
@SuppressLint({ "NewApi", "ServiceCast" })
@SuppressWarnings("deprecation")
public void copyToClipboard(String str) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(str);
} else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(str);
}
Toast toast = Toast.makeText(this, R.string.text_copied_to_clipboard, Toast.LENGTH_SHORT);
toast.show();
}
public boolean isDarkTheme() {
String theme = m_prefs.getString("theme", THEME_DEFAULT);
return theme.equals(THEME_DARK) || theme.equals(THEME_AMBER);
}
protected void setAppTheme(SharedPreferences prefs) {
String theme = prefs.getString("theme", CommonActivity.THEME_DEFAULT);
if (theme.equals(THEME_DARK)) {
setTheme(R.style.DarkTheme);
} else if (theme.equals(THEME_AMBER)) {
setTheme(R.style.AmberTheme);
} else if (theme.equals(THEME_SEPIA)) {
setTheme(R.style.SepiaTheme);
} else {
setTheme(R.style.LightTheme);
}
}
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
protected int getScreenWidthInPixel() {
Display display = getWindowManager().getDefaultDisplay();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {
Point size = new Point();
display.getSize(size);
int width = size.x;
return width;
} else {
return display.getWidth();
}
}
}
|