Java tutorial
/*Copyright 2015 TommyLemon(https://github.com/TommyLemon) 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 zuo.biao.library.base; import java.util.ArrayList; import java.util.List; import zuo.biao.library.R; import zuo.biao.library.interfaces.ActivityPresenter; import zuo.biao.library.interfaces.OnBottomDragListener; import zuo.biao.library.manager.ThreadManager; import zuo.biao.library.ui.EditTextManager; import zuo.biao.library.util.Log; import zuo.biao.library.util.ScreenUtil; import zuo.biao.library.util.StringUtil; import android.app.ProgressDialog; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.Window; import android.widget.Toast; /**android.support.v4.app.FragmentActivity?? ? * *onFling??? * @author Lemon * @see ActivityPresenter#getActivity * @see #context * @see #view * @see #fragmentManager * @see #setContentView * @see #runUiThread * @see #runThread * @see #onDestroy * @use extends BaseActivity, ? .DemoActivity .DemoFragmentActivity */ public abstract class BaseActivity extends FragmentActivity implements ActivityPresenter, OnGestureListener, OnTouchListener { private static final String TAG = "BaseActivity"; /** * Activity??context??context?context * @warn ?? */ protected BaseActivity context = null; /** * Activity??contentView * @warn ?? */ protected View view = null; /** * * @warn ?? */ protected LayoutInflater inflater = null; /** * Fragment? * @warn ?? */ protected FragmentManager fragmentManager = null; private boolean isAlive = false; private boolean isRunning = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); context = getActivity(); isAlive = true; fragmentManager = getSupportFragmentManager(); inflater = getLayoutInflater(); gestureDetector = new GestureDetector(this, this);//?? threadNameList = new ArrayList<String>(); } //???<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< private OnBottomDragListener onBottomDragListener; private GestureDetector gestureDetector; /**Activity??? * @param layoutResID * @param listener * @use ? * *1.onCreatesuper.onCreate?setContentView(layoutResID, this); * *2.?onDragBottom? * *3.??onClickonDragBottom */ public void setContentView(int layoutResID, OnBottomDragListener listener) { super.setContentView(layoutResID); onBottomDragListener = listener; view = inflater.inflate(layoutResID, null); view.setOnTouchListener(this); } //???>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> /** * activity?activity???? */ protected Intent intent = null; /** * activityfragment?intent */ protected Bundle bundle = null; /** * ??,?finish();???? */ protected int enterAnim = R.anim.fade; /** * ?,?finish();???? */ protected int exitAnim = R.anim.right_push_out; /** * */ protected ProgressDialog progressDialog = null; /** * activity????finish? */ protected View toGetWindowTokenView = null; // /**id??? // * @param id // * @return // */ // @SuppressWarnings("unchecked") // public <V extends View> V findViewById(int id) { // return (V) view.findViewById(id); // } /**id?setOnClickListener * @param id * @param l * @return */ @SuppressWarnings("unchecked") public <V extends View> V findViewById(int id, OnClickListener l) { V v = (V) findViewById(id); v.setOnClickListener(l); return v; } //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**?, * @param stringResId */ public void showProgressDialog(int stringResId) { try { showProgressDialog(null, context.getResources().getString(stringResId)); } catch (Exception e) { Log.e(TAG, "showProgressDialog showProgressDialog(null, context.getResources().getString(stringResId));"); } } /**?, * @param dialogMessage */ public void showProgressDialog(String dialogMessage) { showProgressDialog(null, dialogMessage); } /**? * @param dialogTitle * @param dialogMessage ? */ public void showProgressDialog(final String dialogTitle, final String dialogMessage) { runUiThread(new Runnable() { @Override public void run() { if (progressDialog == null) { progressDialog = new ProgressDialog(context); } if (progressDialog.isShowing() == true) { progressDialog.dismiss(); } if (dialogTitle != null && !"".equals(dialogTitle.trim())) { progressDialog.setTitle(dialogTitle); } if (dialogMessage != null && !"".equals(dialogMessage.trim())) { progressDialog.setMessage(dialogMessage); } progressDialog.setCanceledOnTouchOutside(false); progressDialog.show(); } }); } /**?? */ public void dismissProgressDialog() { runUiThread(new Runnable() { @Override public void run() { //runOnUiThread?dismiss???progressDialog.isShowing()? if (progressDialog == null || progressDialog.isShowing() == false) { Log.w(TAG, "dismissProgressDialog progressDialog == null" + " || progressDialog.isShowing() == false >> return;"); return; } progressDialog.dismiss(); } }); } //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //?Activity<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**Activity? * @param intent */ public void toActivity(final Intent intent) { toActivity(intent, true); } /**Activity * @param intent * @param showAnimation */ public void toActivity(final Intent intent, final boolean showAnimation) { toActivity(intent, -1, showAnimation); } /**Activity? * @param intent * @param requestCode */ public void toActivity(final Intent intent, final int requestCode) { toActivity(intent, requestCode, true); } /**Activity * @param intent * @param requestCode * @param showAnimation */ public void toActivity(final Intent intent, final int requestCode, final boolean showAnimation) { runUiThread(new Runnable() { @Override public void run() { if (intent == null) { Log.w(TAG, "toActivity intent == null >> return;"); return; } //fragmentcontext.startActivityfragment?onActivityResult if (requestCode < 0) { startActivity(intent); } else { startActivityForResult(intent, requestCode); } if (showAnimation) { overridePendingTransition(R.anim.right_push_in, R.anim.hold); } else { overridePendingTransition(R.anim.null_anim, R.anim.null_anim); } } }); } //?Activity>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //show short toast <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**?short toast?long toast Toast.makeText(string, Toast.LENGTH_LONG).show(); ---?? * @param stringResId */ public void showShortToast(int stringResId) { try { showShortToast(context.getResources().getString(stringResId)); } catch (Exception e) { Log.e(TAG, "showShortToast context.getResources().getString(resId)" + " >> catch (Exception e) {" + e.getMessage()); } } /**?short toast?long toast Toast.makeText(string, Toast.LENGTH_LONG).show(); ---?? * @param string */ public void showShortToast(final String string) { showShortToast(string, false); } /**?short toast?long toast Toast.makeText(string, Toast.LENGTH_LONG).show(); ---?? * @param string * @param isForceDismissProgressDialog */ public void showShortToast(final String string, final boolean isForceDismissProgressDialog) { runUiThread(new Runnable() { @Override public void run() { if (isForceDismissProgressDialog) { dismissProgressDialog(); } Toast.makeText(context, "" + string, Toast.LENGTH_SHORT).show(); } }); } //show short toast >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //? <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**UI?runOnUiThread * @param action */ public final void runUiThread(Runnable action) { if (isAlive() == false) { Log.w(TAG, "runUiThread isAlive() == false >> return;"); return; } runOnUiThread(action); } /** * ?? */ protected List<String> threadNameList; /**? * @param name * @param runnable * @return */ public final Handler runThread(String name, Runnable runnable) { if (isAlive() == false) { Log.w(TAG, "runThread isAlive() == false >> return null;"); return null; } name = StringUtil.getTrimedString(name); Handler handler = ThreadManager.getInstance().runThread(name, runnable); if (handler == null) { Log.e(TAG, "runThread handler == null >> return null;"); return null; } if (threadNameList.contains(name) == false) { threadNameList.add(name); } return handler; } //? >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> @Override public final boolean isAlive() { return isAlive && context != null;// & ! isFinishing();finishonDestroyrunUiThread?? } @Override public final boolean isRunning() { return isRunning & isAlive(); } @Override public void finish() { super.finish();//?? runUiThread(new Runnable() { @Override public void run() { if (toGetWindowTokenView != null) { EditTextManager.hideKeyboard(context, toGetWindowTokenView); } if (enterAnim > 0 && exitAnim > 0) { try { overridePendingTransition(enterAnim, exitAnim); } catch (Exception e) { Log.e(TAG, "finish overridePendingTransition(enterAnim, exitAnim);" + " >> catch (Exception e) { " + e.getMessage()); } } } }); } @Override protected void onResume() { super.onResume(); isRunning = true; } @Override protected void onPause() { super.onPause(); isRunning = false; } /**? * @warn ?????onDestroysuper.onDestroy();?? */ @Override protected void onDestroy() { dismissProgressDialog(); ThreadManager.getInstance().destroyThread(threadNameList); if (view != null) { try { view.destroyDrawingCache(); } catch (Exception e) { Log.w(TAG, "onDestroy try { view.destroyDrawingCache();" + " >> } catch (Exception e) {\n" + e.getMessage()); } } isAlive = false; isRunning = false; super.onDestroy(); inflater = null; view = null; toGetWindowTokenView = null; fragmentManager = null; progressDialog = null; threadNameList = null; intent = null; bundle = null; context = null; } //?????<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< private boolean isOnKeyLongPress = false; @Override public boolean onKeyLongPress(int keyCode, KeyEvent event) { isOnKeyLongPress = true; return true; } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { if (isOnKeyLongPress) { isOnKeyLongPress = false; return true; } switch (keyCode) { case KeyEvent.KEYCODE_BACK: if (onBottomDragListener != null) { onBottomDragListener.onDragBottom(false); return true; } break; case KeyEvent.KEYCODE_MENU: if (onBottomDragListener != null) { onBottomDragListener.onDragBottom(true); return true; } break; default: break; } return super.onKeyUp(keyCode, event); } //?????>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //???<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @Override public boolean onDown(MotionEvent e) { return false; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { return false; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } @Override public void onLongPress(MotionEvent e) { } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // /*??OnFinishListener? // * onBottomDragListener.onDragBottom(false);onFinishListener.finish();**/ // if (onFinishListener != null) { // // float maxDragHeight = getResources().getDimension(R.dimen.page_drag_max_height); // float distanceY = e2.getRawY() - e1.getRawY(); // if (distanceY < maxDragHeight && distanceY > - maxDragHeight) { // // float minDragWidth = getResources().getDimension(R.dimen.page_drag_min_width); // float distanceX = e2.getRawX() - e1.getRawX(); // if (distanceX > minDragWidth) { // onFinishListener.finish(); // return true; // } // } // } //??? if (onBottomDragListener != null && e1.getRawY() > ScreenUtil.getScreenSize(this)[1] - ((int) getResources().getDimension(R.dimen.bottom_drag_height))) { float maxDragHeight = getResources().getDimension(R.dimen.bottom_drag_max_height); float distanceY = e2.getRawY() - e1.getRawY(); if (distanceY < maxDragHeight && distanceY > -maxDragHeight) { float minDragWidth = getResources().getDimension(R.dimen.bottom_drag_min_width); float distanceX = e2.getRawX() - e1.getRawX(); if (distanceX > minDragWidth) { onBottomDragListener.onDragBottom(false); return true; } else if (distanceX < -minDragWidth) { onBottomDragListener.onDragBottom(true); return true; } } } return false; } @Override public boolean dispatchTouchEvent(MotionEvent ev) { gestureDetector.onTouchEvent(ev); return super.dispatchTouchEvent(ev); } @Override public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); } //???>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> }