/
BaseActivity.java
360 lines (314 loc) · 12.9 KB
/
BaseActivity.java
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Copyright 2011 Google Inc.
*
* 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.bigpupdev.synodroid.ui;
import com.bigpupdev.synodroid.R;
import com.bigpupdev.synodroid.Synodroid;
import com.bigpupdev.synodroid.adapter.SlidingMenuAdapter;
import com.bigpupdev.synodroid.server.SynoServer;
import com.bigpupdev.synodroid.utils.ActionModeHelper;
import com.bigpupdev.synodroid.utils.ActivityHelper;
import com.bigpupdev.synodroid.utils.SlidingMenuItem;
import com.slidingmenu.lib.SlidingMenu;
import de.keyboardsurfer.android.widget.crouton.Crouton;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* A base activity that defers common functionality across app activities to an
* {@link ActivityHelper}. This class shouldn't be used directly; instead, activities should
* inherit from {@link BaseSinglePaneActivity} or {@link BaseMultiPaneActivity}.
*/
public abstract class BaseActivity extends FragmentActivity {
final ActivityHelper mActivityHelper = ActivityHelper.createInstance(this);
final ActionModeHelper mActionModeHelper = ActionModeHelper.createInstance();
private static final int SMNU_DL = 0;
private static final int SMNU_BR = 1;
private static final int SMNU_FI = 2;
private static final int SMNU_RS = 3;
private static final int SMNU_SE = 4;
private static final int SMNU_AB = 5;
private static final int SMNU_SET = 6;
protected SlidingMenu menu = null;
private SlidingMenuItem menuListSelectedItem = null;
public SlidingMenu getSlidingMenu(){
return menu;
};
/**
* Show the preference activity
*/
public void showSearchActivity(boolean clear) {
Intent next = new Intent();
if (clear) next.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
next.setClass(this, SearchActivity.class);
next.putExtra("start_search", true);
startActivity(next);
}
public void setServerChangeListener(OnClickListener ocl){
if (menu == null) return;
RelativeLayout changeSrv = (RelativeLayout) menu.findViewById(R.id.lServer);
changeSrv.setOnClickListener(ocl);
ImageView changeSrvImg = (ImageView) menu.findViewById(R.id.ivChangeSrv);
changeSrvImg.setVisibility(View.VISIBLE);
}
public void updateSMServer(SynoServer server){
if (menu == null) return;
TextView svName = (TextView) menu.findViewById(R.id.tvSrvName);
TextView svURL = (TextView) menu.findViewById(R.id.tvSrvUrl);
if (server != null && server.isConnected()){
svName.setText(server.getNickname());
svURL.setText(server.getUser());
}
else{
svName.setText(R.string.empty_not_connected);
svURL.setText("");
}
}
@Override
protected void onResume() {
super.onResume();
//Make sure the menu is hidden for when the back button is pressed
if (menu != null) menu.showContent(false);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// ignore orientation change
super.onConfigurationChanged(newConfig);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mActivityHelper.onPostCreate(savedInstanceState);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return mActivityHelper.onKeyDown(keyCode, event) ||
super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return mActivityHelper.onCreateOptionsMenu(menu) || super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return mActivityHelper.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
if (menu != null && menu.isMenuShowing()) {
menu.showContent();
} else {
super.onBackPressed();
}
}
public void attachSecondarySlidingMenu(){
if (menu != null){
menu.setMode(SlidingMenu.LEFT_RIGHT);
menu.setSecondaryMenu(R.layout.sliding_bookmarks);
menu.setSecondaryShadowDrawable(R.drawable.shadow_right);
}
}
public void attachSlidingMenu(){
attachSlidingMenu(null);
}
public void attachSlidingMenu(SynoServer srv){
// configure the SlidingMenu
menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.LEFT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setFadeDegree(0.90f);
menu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
menu.setMenu(R.layout.home_menu_frame);
SlidingMenuAdapter adapter = new SlidingMenuAdapter(this);
adapter.add(new SlidingMenuItem(getString(R.string.sliding_downloads), R.drawable.ic_download, SMNU_DL));
adapter.add(new SlidingMenuItem(getString(R.string.sliding_browser), R.drawable.ic_browser, SMNU_BR));
adapter.add(new SlidingMenuItem(getString(R.string.sliding_files), R.drawable.ic_file, SMNU_FI));
//adapter.add(new SlidingMenuItem(getString(R.string.sliding_rss), R.drawable.ic_rss, SMNU_RS));
adapter.add(new SlidingMenuItem(getString(R.string.sliding_search), R.drawable.ic_title_search, SMNU_SE));
adapter.add(new SlidingMenuItem(getString(R.string.menu_parameter), R.drawable.ic_settings, SMNU_SET));
adapter.add(new SlidingMenuItem(getString(R.string.sliding_about), R.drawable.ic_about, SMNU_AB));
final ListView menuList = (ListView) menu.findViewById(R.id.lvMenu);
final Activity act = this;
menuList.setAdapter(adapter);
menuList.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
try {
menuListSelectedItem = (SlidingMenuItem) menuList.getItemAtPosition(i);
switch (menuListSelectedItem.id){
default:
case SMNU_DL:
try{
if (((Synodroid)getApplication()).DEBUG) Log.v(Synodroid.DS_TAG,"SlidingMenu: Menu download selected.");
}catch (Exception ex){/*DO NOTHING*/}
if (!(act instanceof HomeActivity)){
final Intent intent = new Intent(act, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
act.startActivity(intent);
}
else{
menu.showContent(true);
}
break;
case SMNU_BR:
try{
if (((Synodroid)getApplication()).DEBUG) Log.v(Synodroid.DS_TAG,"SlidingMenu: Menu browser selected.");
}catch (Exception ex){/*DO NOTHING*/}
if (!(act instanceof BrowserActivity)){
final Intent intent = new Intent(act, BrowserActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
act.startActivity(intent);
}
else{
menu.showContent(true);
}
break;
case SMNU_FI:
try{
if (((Synodroid)getApplication()).DEBUG) Log.v(Synodroid.DS_TAG,"SlidingMenu: Menu file selected.");
}catch (Exception ex){/*DO NOTHING*/}
if (!(act instanceof FileActivity)){
final Intent intent = new Intent(act, FileActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
act.startActivity(intent);
}
else{
menu.showContent(true);
}
break;
case SMNU_RS:
menu.showContent(true);
Crouton.makeText(BaseActivity.this, R.string.not_yet_implemented, Synodroid.CROUTON_ALERT).show();
break;
case SMNU_SE:
try{
if (((Synodroid)getApplication()).DEBUG) Log.v(Synodroid.DS_TAG,"SlidingMenu: Menu search selected.");
}catch (Exception ex){/*DO NOTHING*/}
if (!(act instanceof SearchActivity)){
showSearchActivity(true);
}
else {
menu.showContent(true);
}
break;
case SMNU_AB:
try{
if (((Synodroid)getApplication()).DEBUG) Log.v(Synodroid.DS_TAG,"SlidingMenu: Menu about selected.");
}catch (Exception ex){/*DO NOTHING*/}
if (!(act instanceof AboutActivity)){
// Starting new intent
Intent next = new Intent();
next.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
next.setClass(act, AboutActivity.class);
startActivity(next);
}
else{
menu.showContent(true);
}
break;
case SMNU_SET:
try{
if (((Synodroid)getApplication()).DEBUG) Log.v(Synodroid.DS_TAG,"SlidingMenu: Menu settings selected.");
}catch (Exception ex){/*DO NOTHING*/}
Intent next = new Intent();
next.setClass(act, DownloadPreferenceActivity.class);
startActivity(next);
break;
}
}
catch(Exception e) {
Log.e(Synodroid.DS_TAG, "Cannot get item at position "+i+".", e);
}
}
});
if (srv != null){
updateSMServer(srv);
}
}
/**
* Returns the {@link ActivityHelper} object associated with this activity.
*/
public ActivityHelper getActivityHelper() {
return mActivityHelper;
}
/**
* Returns the {@link ActivityHelper} object associated with this activity.
*/
public ActionModeHelper getActionModeHelper() {
return mActionModeHelper;
}
/**
* Takes a given intent and either starts a new activity to handle it (the default behavior),
* or creates/updates a fragment (in the case of a multi-pane activity) that can handle the
* intent.
*
* Must be called from the main (UI) thread.
*/
public void openActivityOrFragment(Intent intent) {
// Default implementation simply calls startActivity
startActivity(intent);
}
/**
* Converts an intent into a {@link Bundle} suitable for use as fragment arguments.
*/
public static Bundle intentToFragmentArguments(Intent intent) {
Bundle arguments = new Bundle();
if (intent == null) {
return arguments;
}
final Uri data = intent.getData();
if (data != null) {
arguments.putParcelable("_uri", data);
}
final Bundle extras = intent.getExtras();
if (extras != null) {
arguments.putAll(intent.getExtras());
}
return arguments;
}
/**
* Converts a fragment arguments bundle into an intent.
*/
public static Intent fragmentArgumentsToIntent(Bundle arguments) {
Intent intent = new Intent();
if (arguments == null) {
return intent;
}
final Uri data = arguments.getParcelable("_uri");
if (data != null) {
intent.setData(data);
}
intent.putExtras(arguments);
intent.removeExtra("_uri");
return intent;
}
}