Back to project page SimpleReader.
The source code is released under:
Apache License
If you think the Android project SimpleReader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.dreamteam.app.ui; // ww w . ja v a2 s. c o m import java.io.File; import java.util.ArrayList; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Point; import android.graphics.drawable.BitmapDrawable; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.GridView; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import com.dreamteam.app.adapter.GridAdapter; import com.dreamteam.app.adapter.MPagerAdapter; import com.dreamteam.app.commons.AppConfig; import com.dreamteam.app.commons.AppContext; import com.dreamteam.app.commons.ItemListEntityParser; import com.dreamteam.app.commons.SectionHelper; import com.dreamteam.app.commons.SeriaHelper; import com.dreamteam.app.commons.SkinManager; import com.dreamteam.app.commons.UIHelper; import com.dreamteam.app.db.DbManager; import com.dreamteam.app.entity.ItemListEntity; import com.dreamteam.app.entity.Section; import com.dreamteam.app.utils.ImageUtils; import com.dreamteam.app.utils.Logger; import com.dreamteam.app.wallpaper.NativeImageLoader; import com.dreamteam.app.wallpaper.NativeImageLoader.OnImageLoadListener; import com.dreamteam.custom.ui.PathAnimations; import com.dreateam.app.ui.R; import com.umeng.socialize.controller.UMInfoAgent; public class MainActivity extends BaseActivity { public static final String tag = "Main"; private ViewPager mPager; private MPagerAdapter mPagerAdapter; private RelativeLayout composerWrapper; private RelativeLayout composerShowHideBtn; private RelativeLayout bgLayout; private ImageView composerShowHideIconIv; private TextView pageTv; private ImageButton switchModeBtn; private RelativeLayout homeLoadingLayout; private ArrayList<GridView> gridViews = new ArrayList<GridView>(); private ArrayList<GridAdapter> gridAdapters = new ArrayList<GridAdapter>(); private BroadcastReceiver mReceiver; private boolean arePathMenuShowing; public static final int PAGE_SECTION_SIZE = 3;// ??8?section public static final String ACTION_ADD_SECTION = "com.dreamteam.app.action.add_section"; public static final String ACTION_DELETE_SECTION = "com.dreamteam.app.action.delete_section"; public static final String ACTION_SET_BG = "com.dreamteam.app.action.set_bg"; public static final int PAGE_SIZE_INCREASE = 1; public static final int PAGE_SIZE_NOT_CHANGE = 0; public static final int PAGE_SIZE_DECREASE = -1; private Intent mIntent; private boolean exit = false;// ?????? private boolean isEdting = false;// ??????section? private boolean isNight;// ?????????? private SharedPreferences sp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initParams(); checkShortcutMsg(); initView(); initPathMenu(); initPager(); initBroadcast(); checkDeprecated(); } private void initParams() { this.sp = getSharedPreferences(SkinManager.SP_SKIN, Context.MODE_PRIVATE); isNight = sp.getBoolean(SkinManager.SP_SKIN_TYPE_IS_NIGHT, false); } // ??????????shortcut??? private void checkShortcutMsg() { Intent intent = getIntent(); if (intent != null) { String action = intent.getAction(); if (action != null && action.equals(GridAdapter.ACTION_ENTER_BY_SHORTCUT)) { Intent indirectIntent = new Intent(); indirectIntent.putExtra("section_title", intent.getStringExtra("section_title")); indirectIntent.putExtra("url", intent.getStringExtra("url")); indirectIntent.setClass(this, ItemListActivity.class); startActivity(indirectIntent); } } } private void initBroadcast() { mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Logger.error("tangl", action); if (action.equals(ACTION_ADD_SECTION)) { // ??????adapter??????????gridView Log.d(tag, gridAdapters.size() + "adapters"); Log.d(tag, gridViews.size() + "views"); GridAdapter lastGridAdapter = getLastGridAdapter(); if (lastGridAdapter == null || lastGridAdapter.isFull()) { addGridView(); } else { // ??????gridAdapter??section lastGridAdapter.addItem(getNewSection()); } } else if (action.equals(ACTION_DELETE_SECTION)) { // ???????section GridAdapter deCreaseAdapter = null; String url = intent.getStringExtra("url"); for (int i = 0; i < gridAdapters.size(); i++) { deCreaseAdapter = gridAdapters.get(i); if (deCreaseAdapter.removeItem(url)) { break; } } GridAdapter lastAdapter = getLastGridAdapter(); if (lastAdapter.isEmpty()) { if (gridViews.size() <= 1) { return; } removeLastGridView(); return; } if (!lastAdapter.equals(deCreaseAdapter)) { Section section = lastAdapter.getLastItem(); deCreaseAdapter.addItem(section); lastAdapter.removeItem(section.getUrl()); } if (lastAdapter.isEmpty()) { if (gridViews.size() <= 1) { return; } removeLastGridView(); removeLastGridAdapter(); } } else if (action.equals(SwitchBgActivity.SWITCH_HOME_BG)) { int resid = intent.getIntExtra("home_bg_id", R.drawable.home_bg_default); bgLayout.setBackgroundResource(resid); Editor editor = AppContext.getPrefrences(MainActivity.this) .edit(); editor.putInt("home_bg_id", resid); editor.commit(); } else if (action.equals(ACTION_SET_BG)) { if(intent != null) { Bundle data = intent.getExtras(); String path = data.getString("path"); setHOmeImageViewBackground(path); } } } }; IntentFilter filter = new IntentFilter(); filter.addAction(ACTION_ADD_SECTION); filter.addAction(ACTION_DELETE_SECTION); filter.addAction(SwitchBgActivity.SWITCH_HOME_BG); filter.addAction(ACTION_SET_BG); registerReceiver(mReceiver, filter); } // ?????????section private Section getNewSection() { Section section = new Section(); DbManager mgr = new DbManager(MainActivity.this, DbManager.DB_NAME, null, 1); SQLiteDatabase db = mgr.getWritableDatabase(); Cursor cursor = db.query(DbManager.SECTION_TABLE_NAME, null, null, null, null, null, null); if (cursor.moveToLast()) { String title = cursor.getString(cursor.getColumnIndex("title")); String url = cursor.getString(cursor.getColumnIndex("url")); String tableName = cursor.getString(cursor .getColumnIndex("table_name")); section.setTitle(title); section.setUrl(url); section.setTableName(tableName); } db.close(); return section; } private void initPathMenu() { PathAnimations.initOffset(this); composerWrapper = (RelativeLayout) findViewById(R.id.composer_wrapper); composerShowHideBtn = (RelativeLayout) findViewById(R.id.composer_show_hide_button); composerShowHideIconIv = (ImageView) findViewById(R.id.composer_show_hide_button_icon); composerShowHideBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (!arePathMenuShowing) { PathAnimations.startAnimationsIn(composerWrapper, 300); composerShowHideIconIv.startAnimation(PathAnimations .getRotateAnimation(0, -270, 300)); } else { PathAnimations.startAnimationsOut(composerWrapper, 300); composerShowHideIconIv.startAnimation(PathAnimations .getRotateAnimation(-270, 0, 300)); } arePathMenuShowing = !arePathMenuShowing; } }); // Buttons?????? for (int i = 0; i < composerWrapper.getChildCount(); i++) { composerWrapper.getChildAt(i).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.composer_btn_user: login(); break; case R.id.composer_btn_setting: openSetting(); break; case R.id.composer_btn_favorite: openFavorite(); break; case R.id.composer_btn_switch_bg: swithBg(); break; case R.id.composer_btn_add: openSubscribeCenter(); break; case R.id.composer_btn_moon: switchMode(); break; } } }); } composerShowHideBtn.startAnimation(PathAnimations.getRotateAnimation(0, 360, 200)); } private void switchMode() { SharedPreferences sp = getSharedPreferences(SkinManager.SP_SKIN, Context.MODE_PRIVATE); Editor editor = sp.edit(); // ??????? if (isNight) { isNight = false; bgLayout.setBackgroundResource(R.drawable.home_bg_default); switchModeBtn.setImageResource(R.drawable.composer_sun); // ??????? } else { isNight = true; bgLayout.setBackgroundResource(R.drawable.home_bg_night); switchModeBtn.setImageResource(R.drawable.composer_moon); } editor.putBoolean(SkinManager.SP_SKIN_TYPE_IS_NIGHT, isNight); editor.commit(); } // ??????? private void swithBg() { Intent intent = new Intent(); intent.setClass(MainActivity.this, SwitchBgActivity.class); MainActivity.this.startActivity(intent); } // ????? private void openFavorite() { Intent intent = new Intent(); intent.setClass(MainActivity.this, FavoriteItemList.class); MainActivity.this.startActivity(intent); } // ???????? private void openSetting() { Intent intent = new Intent(); intent.setClass(MainActivity.this, Setting.class); MainActivity.this.startActivity(intent); } // ?? private void login() { if (UMInfoAgent.isLogin(this)) { Toast.makeText(this, "??????", Toast.LENGTH_SHORT).show(); return; } new LoginDialog().show(getSupportFragmentManager(), tag); } // ?????? private void openSubscribeCenter() { Intent intent = new Intent(); intent.setClass(this, FeedCategoryActivity.class); startActivity(intent); } private void initPager() { int pageSize = getPageSize(); for (int i = 0; i < pageSize; i++) { gridViews.add(newGridView(i)); mPagerAdapter.notifyDataSetChanged(); } } private void initView() { UIHelper.initTheme(this); setContentView(R.layout.main); switchModeBtn = (ImageButton) findViewById(R.id.composer_btn_moon); pageTv = (TextView) findViewById(R.id.home_page_tv); homeLoadingLayout = (RelativeLayout) findViewById(R.id.home_loading_layout); bgLayout = (RelativeLayout) findViewById(R.id.home_bg_layout); mPager = (ViewPager) findViewById(R.id.home_pager); String path_bg = sp.getString(AppConfig.SP_KEY_MAIN_BG, ""); if(path_bg != null && !"".equals(path_bg)) { setHOmeImageViewBackground(path_bg); } mPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position) { } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { pageTv.setText(position + 1 + ""); } @Override public void onPageScrollStateChanged(int state) { } }); mPagerAdapter = new MPagerAdapter(gridViews); mPager.setAdapter(mPagerAdapter); } private GridView newGridView(int currentPage) { GridView grid = new GridView(this); LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); grid.setLayoutParams(params); // int right = ImageUtils.dip2px(this, 50); // int left = ImageUtils.dip2px(this, 20); // int top = ImageUtils.dip2px(this, 20); // int bottom = ImageUtils.dip2px(this, 20); // grid.setPadding(left, top, right, bottom); grid.setNumColumns(1); grid.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { GridAdapter adapter = gridAdapters.get(mPager.getCurrentItem()); Section section = (Section) adapter.getItem(position); String title = section.getTitle(); String url = section.getUrl(); Log.d(tag, url); // ???intent mIntent = new Intent(); mIntent.putExtra("section_title", title); mIntent.putExtra("url", url); mIntent.setClass(MainActivity.this, ItemListActivity.class); // ?????? File cache = SectionHelper.getSdCache(url); if (cache.exists()) { MainActivity.this.startActivity(mIntent); } else { if (!AppContext.isNetworkAvailable(MainActivity.this)) { Toast.makeText(MainActivity.this, R.string.no_network, Toast.LENGTH_SHORT).show(); return; } // ???????? Log.d(tag, "" + url); new LoadDataTask().execute(url); } } }); // ??????section??? grid.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { inSectionEdit(); return false; } }); ArrayList<Section> sections = null; try { sections = readSections(currentPage); } catch (Exception e) { e.printStackTrace(); } GridAdapter gridAdapter = new GridAdapter(this, sections); gridAdapters.add(gridAdapter); grid.setAdapter(gridAdapter); return grid; } private void inSectionEdit() { isEdting = true; int isVisble = 1; // section????????? for (int i = 0; i < gridViews.size(); i++) { gridViews.get(i).setEnabled(false); } for (int i = 0; i < gridAdapters.size(); i++) { gridAdapters.get(i).changeDelBtnState(isVisble); } Toast.makeText(this, "????????????", Toast.LENGTH_SHORT).show(); } // ??????? private void outSectionEdit() { isEdting = false; int isVisble = 0; for (int i = 0; i < gridViews.size(); i++) { gridViews.get(i).setEnabled(true); } for (int i = 0; i < gridAdapters.size(); i++) { gridAdapters.get(i).changeDelBtnState(isVisble); } } private ArrayList<Section> readSections(int page) throws Exception { ArrayList<Section> sections = null; int len = 0;// ?? int start = 0;// ??? int end = 0;// ?? Log.i(tag, "page = " + page); // ??????????? DbManager mgr = new DbManager(MainActivity.this, DbManager.DB_NAME, null, 1); SQLiteDatabase db = mgr.getWritableDatabase(); Cursor cursor = db.query(DbManager.SECTION_TABLE_NAME, null, null, null, null, null, null); len = cursor.getCount(); db.close(); start = page * MainActivity.PAGE_SECTION_SIZE; if (cursor.moveToPosition(start)) { sections = new ArrayList<Section>(); int offset = start + MainActivity.PAGE_SECTION_SIZE; end = len < offset ? len : offset; for (int i = start; i < end; i++) { Section s = new Section(); String title = cursor.getString(cursor.getColumnIndex("title")); String url = cursor.getString(cursor.getColumnIndex("url")); String tableName = cursor.getString(cursor .getColumnIndex("table_name")); s.setTitle(title); s.setUrl(url); s.setTableName(tableName); sections.add(s); cursor.moveToNext(); } } return sections; } @Override protected void onDestroy() { super.onDestroy(); // ???????? unregisterReceiver(mReceiver); } private void addGridView() { int lastPage = getPageSize() - 1; GridView grid = newGridView(lastPage); gridViews.add(grid); mPagerAdapter.notifyDataSetChanged(); } private void removeLastGridView() { if (gridViews.isEmpty()) return; gridViews.remove(gridViews.size() - 1); mPagerAdapter.notifyDataSetChanged(); } private GridAdapter getLastGridAdapter() { if (gridAdapters.isEmpty()) return null; return gridAdapters.get(gridAdapters.size() - 1); } private void removeLastGridAdapter() { if (gridAdapters.isEmpty()) return; gridAdapters.remove(gridAdapters.size() - 1); } // ?1? private int getPageSize() { // ??????????? DbManager mgr = new DbManager(MainActivity.this, DbManager.DB_NAME, null, 1); SQLiteDatabase db = mgr.getWritableDatabase(); Cursor cursor = db.query(DbManager.SECTION_TABLE_NAME, null, null, null, null, null, null); // pager?? int pageSize = 0; int sectionCount = cursor.getCount(); db.close(); if (sectionCount % PAGE_SECTION_SIZE == 0) pageSize = sectionCount / PAGE_SECTION_SIZE; else pageSize = sectionCount / PAGE_SECTION_SIZE + 1; return pageSize; } private class LoadDataTask extends AsyncTask<String, Integer, ItemListEntity> { @Override protected void onPreExecute() { homeLoadingLayout.setVisibility(View.VISIBLE); } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); } @Override protected void onPostExecute(ItemListEntity result) { homeLoadingLayout.setVisibility(View.GONE); // ?????? if (result != null && mIntent != null && !result.getItemList().isEmpty()) { MainActivity.this.startActivity(mIntent); } else { Toast.makeText(MainActivity.this, "??????", Toast.LENGTH_SHORT) .show(); } } @Override protected ItemListEntity doInBackground(String... params) { ItemListEntityParser parser = new ItemListEntityParser(); ItemListEntity entity = parser.parse(params[0]); if (entity != null) { SeriaHelper helper = SeriaHelper.newInstance(); File cache = SectionHelper.newSdCache(params[0]); helper.saveObject(entity, cache); } return entity; } } // ??true?????????????????????????????????????????? // ????false?????????????????????????????????????? // ??Activity?????? @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (isEdting) { // ?????????? outSectionEdit(); } else { if (exit) { Log.d(tag, "exit"); finish(); return true; } Toast.makeText(this, "?????????", Toast.LENGTH_SHORT).show(); exit = true; Log.d(tag, "after toast"); } } return false; } /** * @description ???????????? */ private void checkDeprecated() { String fileName = getFilesDir().getAbsolutePath() + File.separator + AppConfig.PREF_DEPRECATED; File file = new File(fileName); int day = (int) (System.currentTimeMillis() - file.lastModified()) / (24 * 60 * 60 * 1000); Log.d(tag, "day = " + day); if (day >= 7) { AppContext.clearCache(this); file.setLastModified(System.currentTimeMillis()); } } ////////////////////////////////////////////////////////////// // common methods ////////////////////////////////////////////////////////////// private void setHOmeImageViewBackground(String path) { if(path != null && !"".equals(path)) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; Point point = new Point(600, 800); Bitmap bitmap = NativeImageLoader.getInstance().loadImage(path, point, new OnImageLoadListener() { @Override public void onImageLoad(String path, Bitmap bitmap) { bgLayout.setBackgroundDrawable(new BitmapDrawable(bitmap)); }}); if(bitmap != null) { bgLayout.setBackgroundDrawable(new BitmapDrawable(bitmap)); } } } }