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 robert.com.basiclibrary.base; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.ProgressDialog; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.support.annotation.Nullable; 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.view.WindowManager; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; import robert.com.basiclibrary.R; import robert.com.basiclibrary.interfaces.ActivityPresenter; import robert.com.basiclibrary.interfaces.OnBottomDragListener; import robert.com.basiclibrary.manager.SystemBarTintManager; import robert.com.basiclibrary.manager.ThreadManager; import robert.com.basiclibrary.util.Log; import robert.com.basiclibrary.util.ScreenUtil; import robert.com.basiclibrary.util.StringUtil; /**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 { 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 = (BaseActivity) getActivity(); isAlive = true; fragmentManager = getSupportFragmentManager(); inflater = getLayoutInflater(); threadNameList = new ArrayList<String>(); } /** * activityTextViewlayout.xml@id/tvBaseTitle?ActivityautoSetTitle INTENT_TITLE * @see #autoSetTitle * @warn ?Activitylayoutandroid:id="@id/tvBaseTitle"TextViewtvBaseTitle??Activity */ @Nullable protected TextView tvBaseTitle; @TargetApi(Build.VERSION_CODES.KITKAT) @Override public void setContentView(int layoutResID) { super.setContentView(layoutResID); // ??4.4+ <<<<<<<<<<<<<<<<< if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } SystemBarTintManager tintManager = new SystemBarTintManager(this); tintManager.setStatusBarTintEnabled(true); tintManager.setStatusBarTintResource(R.color.topbar_bg);//??drawable? // ??4.4+ >>>>>>>>>>>>>>>>> tvBaseTitle = (TextView) findViewById(R.id.tvBaseTitle);//TextView } //???<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 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) { setContentView(layoutResID); onBottomDragListener = listener; gestureDetector = new GestureDetector(this, this);//?? view = inflater.inflate(layoutResID, null); view.setOnTouchListener(new OnTouchListener() { @SuppressLint("ClickableViewAccessibility") @Override public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); } }); } //???>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> /** * activity?activity???? */ protected Intent intent = null; /** * ??,?finish();???? */ protected int enterAnim = R.anim.fade; /** * ?,?finish();???? */ protected int exitAnim = R.anim.right_push_out; // /**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; } //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**ActivityINTENT_TITLE?initView * *returntvTitle = tvBaseTitletvBaseTitle * @must UI */ protected void autoSetTitle() { autoSetTitle(tvBaseTitle); } /**ActivityINTENT_TITLE?initView * @param tvTitle * @return tvTitle tvTitle?? tvTitle = autoSetTitle((TextView) findViewById(titleResId)); * @must UI */ protected TextView autoSetTitle(TextView tvTitle) { if (tvTitle != null && StringUtil.isNotEmpty(getIntent().getStringExtra(INTENT_TITLE), false)) { tvTitle.setText(StringUtil.getCurrentString()); } return tvTitle; } //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /** * */ protected ProgressDialog progressDialog = null; /**?, * @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 message */ public void showProgressDialog(String message) { showProgressDialog(null, message); } /**? * @param title * @param message ? */ public void showProgressDialog(final String title, final String message) { runUiThread(new Runnable() { @Override public void run() { if (progressDialog == null) { progressDialog = new ProgressDialog(context); } if (progressDialog.isShowing()) { progressDialog.dismiss(); } if (StringUtil.isNotEmpty(title, false)) { progressDialog.setTitle(title); } if (StringUtil.isNotEmpty(message, false)) { progressDialog.setMessage(message); } 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(Intent intent) { toActivity(intent, true); } /**Activity * @param intent * @param showAnimation */ public void toActivity(Intent intent, boolean showAnimation) { toActivity(intent, -1, showAnimation); } /**Activity? * @param intent * @param requestCode */ public void toActivity(Intent intent, 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(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; } //? >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //Activity????Fragment?????<<<<<<<<< /**?onBottomDragListener.onDragBottom(false)??? * @param v * @use layout.xmlandroid:onClick="onReturnClick"?? * @warn ?ActivitycontentView layout * *View setOnClickListenerandroid:onClick="onReturnClick" */ @Override public void onReturnClick(View v) { Log.d(TAG, "onReturnClick >>>"); if (onBottomDragListener != null) { onBottomDragListener.onDragBottom(false); } else { onBackPressed();//?finish();BaseBottomWindow } } /**??onBottomDragListener.onDragBottom(true)??? * @param v * @use layout.xmlandroid:onClick="onForwardClick"?? * @warn ?ActivitycontentView layout * *View setOnClickListenerandroid:onClick="onForwardClick" */ @Override public void onForwardClick(View v) { Log.d(TAG, "onForwardClick >>>"); if (onBottomDragListener != null) { onBottomDragListener.onDragBottom(true); } } //Activity??BottomWindowFragment?????>>>>> @Override public final boolean isAlive() { return isAlive && context != null;// & ! isFinishing();finishonDestroyrunUiThread?? } @Override public final boolean isRunning() { return isRunning & isAlive(); } /**????onCreate???id(id<=0)?? */ public void finishWithError(String error) { showShortToast(error); enterAnim = exitAnim = R.anim.null_anim; finish(); } @Override public void finish() { super.finish();//?? runUiThread(new Runnable() { @Override public void run() { 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() { Log.d(TAG, "\n onResume <<<<<<<<<<<<<<<<<<<<<<<"); super.onResume(); isRunning = true; Log.d(TAG, "onResume >>>>>>>>>>>>>>>>>>>>>>>>\n"); } @Override protected void onPause() { Log.d(TAG, "\n onPause <<<<<<<<<<<<<<<<<<<<<<<"); super.onPause(); isRunning = false; Log.d(TAG, "onPause >>>>>>>>>>>>>>>>>>>>>>>>\n"); } /**? * @warn ?????onDestroysuper.onDestroy();?? */ @Override protected void onDestroy() { Log.d(TAG, "\n 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; tvBaseTitle = null; fragmentManager = null; progressDialog = null; threadNameList = null; intent = null; context = null; Log.d(TAG, "onDestroy >>>>>>>>>>>>>>>>>>>>>>>>\n"); } //?????<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 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) { if (gestureDetector != null) { gestureDetector.onTouchEvent(ev); } return super.dispatchTouchEvent(ev); } //???>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> }