/
MainActivity.java
310 lines (270 loc) · 12.5 KB
/
MainActivity.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
package org.mewx.wenku8.activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Toast;
import com.afollestad.materialdialogs.GravityEnum;
import com.afollestad.materialdialogs.MaterialDialog;
import com.afollestad.materialdialogs.Theme;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.umeng.analytics.MobclickAgent;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;
import com.umeng.update.UpdateStatus;
import org.mewx.wenku8.R;
import org.mewx.wenku8.fragment.NavigationDrawerFragment;
import org.mewx.wenku8.global.GlobalConfig;
import org.mewx.wenku8.util.LightCache;
import org.mewx.wenku8.util.LightUserSession;
import java.io.File;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
// This is for fragment switch
public enum FRAGMENT_LIST {
RKLIST, LATEST, FAV, CONFIG
}
private FRAGMENT_LIST status = FRAGMENT_LIST.LATEST;
public FRAGMENT_LIST getCurrentFragment() {
return status;
}
public void setCurrentFragment(FRAGMENT_LIST f) {
status = f;
}
private NavigationDrawerFragment mNavigationDrawerFragment;
private static Boolean isExit = false; // used for exit by twice
private void initialApp() {
// load language
Locale locale;
switch (GlobalConfig.getCurrentLang()) {
case SC:
locale = Locale.SIMPLIFIED_CHINESE;
break;
case TC:
locale = Locale.TRADITIONAL_CHINESE;
break;
default:
locale = Locale.SIMPLIFIED_CHINESE;
break;
}
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = locale;
Locale.setDefault(locale);
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
// tint
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setTintAlpha(0.0f);
// execute background action
LightUserSession.aiui = new LightUserSession.AsyncInitUserInfo();
LightUserSession.aiui.execute();
GlobalConfig.loadAllSetting();
// create save folder
LightCache.saveFile(GlobalConfig.getFirstStoragePath() + "imgs", ".nomedia", "".getBytes(), false);
LightCache.saveFile(GlobalConfig.getSecondStoragePath() + "imgs", ".nomedia", "".getBytes(), false);
LightCache.saveFile(GlobalConfig.getFirstStoragePath() + GlobalConfig.customFolderName, ".nomedia", "".getBytes(), false);
LightCache.saveFile(GlobalConfig.getSecondStoragePath() + GlobalConfig.customFolderName, ".nomedia", "".getBytes(), false);
GlobalConfig.setFirstStoragePathStatus(LightCache.testFileExist(GlobalConfig.getFirstStoragePath() + "imgs" + File.separator + ".nomedia"));
// TODO: set status? tell app where is available
LightCache.saveFile(GlobalConfig.getFirstFullSaveFilePath() + "imgs", ".nomedia", "".getBytes(), false);
LightCache.saveFile(GlobalConfig.getSecondFullSaveFilePath() + "imgs", ".nomedia", "".getBytes(), false);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main); // have 3 styles
initialApp();
// UIL setting
if(ImageLoader.getInstance() == null || !ImageLoader.getInstance().isInited()) {
GlobalConfig.initImageLoader(this);
}
// global settings
GlobalConfig.initVolleyNetwork();
// UMeng settings
MobclickAgent.updateOnlineConfig(this);
UmengUpdateAgent.setUpdateCheckConfig(false); // disable res check
UmengUpdateAgent.setDeltaUpdate(false); // useless, same version code has two version
UmengUpdateAgent.setUpdateOnlyWifi(false);
// update dialog show up
UmengUpdateAgent.setUpdateAutoPopup(false);
UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {
@Override
public void onUpdateReturned(int updateStatus, final UpdateResponse updateInfo) {
switch (updateStatus) {
case UpdateStatus.Yes: // has update
if (UmengUpdateAgent.isIgnore(MainActivity.this, updateInfo)) {
//Toast.makeText(MainActivity.this, getResources().getString(R.string.system_update_ignored), Toast.LENGTH_SHORT).show();
break;
} else {
new MaterialDialog.Builder(MainActivity.this)
.theme(Theme.LIGHT)
.callback(new MaterialDialog.ButtonCallback() {
@Override
public void onNegative(MaterialDialog dialog) {
super.onNegative(dialog);
UmengUpdateAgent.ignoreUpdate(MainActivity.this, updateInfo);
}
})
.forceStacking(true)
.titleColorRes(R.color.default_text_color_black)
.backgroundColorRes(R.color.dlgBackgroundColor)
.contentColorRes(R.color.dlgContentColor)
.positiveColorRes(R.color.dlgPositiveButtonColor)
.negativeColorRes(R.color.dlgNegativeButtonColor)
.title("New: " + updateInfo.version)
.content(updateInfo.updateLog)
.titleGravity(GravityEnum.CENTER)
.positiveText(R.string.dialog_positive_gotit)
.negativeText(R.string.dialog_negative_ignore_this_version)
.show();
}
break;
}
}
});
UmengUpdateAgent.update(this);
// Update old save files ----------------
// set Toolbar
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar);
// getSupportActionBar().setDisplayShowHomeEnabled(true);
// set Tool button
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
// find search box
mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
//Toast.makeText(MyApp.getContext(),"called button",Toast.LENGTH_SHORT).show();
if (item.getItemId() == R.id.action_search) {
// start search activity
startActivity(new Intent(MainActivity.this, SearchActivity.class));
overridePendingTransition(R.anim.fade_in, R.anim.hold); // fade in animation
}
return true;
}
});
// change status bar color tint, and this require SDK16
if (Build.VERSION.SDK_INT >= 16 ) { //&& Build.VERSION.SDK_INT <= 21) {
// Android API 22 has more effects on status bar, so ignore
// create our manager instance after the content view is set
SystemBarTintManager tintManager = new SystemBarTintManager(this);
// enable all tint
tintManager.setStatusBarTintEnabled(true);
tintManager.setNavigationBarTintEnabled(true);
tintManager.setTintAlpha(0.15f);
tintManager.setNavigationBarAlpha(0.0f);
// set all color
tintManager.setTintColor(getResources().getColor(android.R.color.black));
// set Navigation bar color
if(Build.VERSION.SDK_INT >= 21)
getWindow().setNavigationBarColor(getResources().getColor(R.color.myNavigationColor));
}
}
/**
* Hard menu button works like the soft menu button.
* And this will control all the menu appearance,
* I can handle the button list by edit this function.
*
* @param menu The options menu in which you place your items, but I ignore this.
* @return True if shown successfully, False if failed.
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// only when the navigation draw closed, I draw the menu bar.
// the menu items will be drawn automatically
if(!mNavigationDrawerFragment.isDrawerOpen()) {
// change title of toolbar
switch(status){
case LATEST:
if(getSupportActionBar()!=null) getSupportActionBar().setTitle(getResources().getString(R.string.main_menu_latest));
getMenuInflater().inflate(R.menu.menu_latest, menu);
break;
case RKLIST:
if(getSupportActionBar()!=null) getSupportActionBar().setTitle(getResources().getString(R.string.main_menu_rklist));
break;
case FAV:
if(getSupportActionBar()!=null) getSupportActionBar().setTitle(getResources().getString(R.string.main_menu_fav));
break;
case CONFIG:
if(getSupportActionBar()!=null) getSupportActionBar().setTitle(getResources().getString(R.string.main_menu_config));
break;
}
}
else {
if(getSupportActionBar()!=null)
getSupportActionBar().setTitle(getResources().getString(R.string.app_name));
}
return true;
}
/**
* This function will be called by NavigationDrawerFragment,
* once called, change fragment.
*
* @param targetFragment target fragment.
*/
public void changeFragment(Fragment targetFragment) {
// temporarily set elevation to remove rank list toolbar shadow
if(getSupportActionBar() != null) {
if (status == FRAGMENT_LIST.RKLIST)
getSupportActionBar().setElevation(0);
else
getSupportActionBar().setElevation(getResources().getDimension(R.dimen.toolbar_elevation));
}
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, targetFragment, "fragment")
.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.commit();
}
@Override
protected void onPause() {
super.onPause();
MobclickAgent.onPageEnd("MainActivity");
MobclickAgent.onPause(this);
}
@Override
protected void onResume() {
super.onResume();
MobclickAgent.onPageStart("MainActivity");
MobclickAgent.onResume(this);
}
@Override
public void onBackPressed() {
if (mNavigationDrawerFragment.isDrawerOpen())
mNavigationDrawerFragment.closeDrawer();
else
exitBy2Click();
}
private void exitBy2Click() {
// press twice to exit
Timer tExit;
if (!isExit) {
isExit = true; // ready to exit
Toast.makeText(
this,
this.getResources().getString(R.string.press_twice_to_exit),
Toast.LENGTH_SHORT).show();
tExit = new Timer();
tExit.schedule(new TimerTask() {
@Override
public void run() {
isExit = false; // cancel exit
}
}, 2000); // 2 seconds cancel exit task
} else {
finish();
// call fragments and end streams and services
System.exit(0);
}
}
}