Java tutorial
/* * Copyright (c) 2013 Cangol * <p> * 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 * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * 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 mobi.cangol.mobile.base; import android.content.Context; import android.content.res.Configuration; import android.content.res.TypedArray; import android.os.Bundle; import android.os.Handler; import android.os.HandlerThread; import android.os.Looper; import android.os.Message; import android.support.annotation.AttrRes; import android.support.annotation.ColorInt; import android.support.v4.app.FragmentActivity; import android.view.KeyEvent; import android.view.WindowManager; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.Toast; import java.lang.ref.WeakReference; import mobi.cangol.mobile.CoreApplication; import mobi.cangol.mobile.logging.Log; import mobi.cangol.mobile.service.AppService; import mobi.cangol.mobile.service.session.SessionService; public abstract class BaseFragmentActivity extends FragmentActivity implements BaseActivityDelegate, CustomFragmentActivityDelegate { protected final String TAG = Log.makeLogTag(this.getClass()); protected CoreApplication app; private CustomFragmentManager stack; private long startTime; private HandlerThread handlerThread; private Handler handler; public float getIdleTime() { return (System.currentTimeMillis() - startTime) / 1000.0f; } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); Log.setLogTag(this); startTime = System.currentTimeMillis(); handlerThread = new HandlerThread(TAG); handlerThread.start(); handler = new InternalHandler(this, handlerThread.getLooper()); app = (CoreApplication) this.getApplication(); app.addActivityToManager(this); } @Override public void showToast(int resId) { if (!isFinishing()) Toast.makeText(this, resId, Toast.LENGTH_SHORT).show(); } @Override public void showToast(String str) { if (!isFinishing()) Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); } @Override public void showToast(int resId, int duration) { if (!isFinishing()) Toast.makeText(this, resId, duration).show(); } @Override public void showToast(String str, int duration) { if (!isFinishing()) Toast.makeText(this, str, duration).show(); } /** * ?? * * @param containerId */ public void initFragmentStack(int containerId) { if (null == stack) stack = CustomFragmentManager.forContainer(this, containerId, this.getSupportFragmentManager()); } /** * ?fragment * * @param fragmentClass * @param tag * @param args */ public void replaceFragment(Class<? extends BaseFragment> fragmentClass, String tag, Bundle args) { if (null == stack) { throw new IllegalStateException("stack is null"); } else if (!stack.isStateSaved()) { stack.replace(fragmentClass, tag, args); stack.commit(); } else { Log.e(TAG, "Can not perform this action after onSaveInstanceState"); } } @Override public CustomFragmentManager getCustomFragmentManager() { return stack; } /** * ?AppService * * @param name * @return */ public AppService getAppService(String name) { return app.getAppService(name); } /** * ?Session * * @return */ public SessionService getSession() { return app.getSession(); } @Override protected void onStart() { super.onStart(); Log.v(TAG, "onStart"); } @Override protected void onResume() { super.onResume(); Log.v(TAG, "onResume " + getIdleTime() + "s"); } @Override protected void onPause() { super.onPause(); Log.v(TAG, "onPause"); } @Override protected void onRestart() { super.onRestart(); Log.v(TAG, "onRestart"); } @Override protected void onStop() { super.onStop(); Log.v(TAG, "onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.v(TAG, "onDestroy"); if (null != stack) stack.destroy(); app.delActivityFromManager(this); handlerThread.quit(); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); if (null != stack) stack.restoreState(savedInstanceState); Log.v(TAG, "onRestoreInstanceState"); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (null != stack) stack.saveState(outState); Log.v(TAG, "onSaveInstanceState"); } @Override public Object getLastCustomNonConfigurationInstance() { Log.v(TAG, "getLastCustomNonConfigurationInstance"); return super.getLastCustomNonConfigurationInstance(); } @Override public Object onRetainCustomNonConfigurationInstance() { Log.v(TAG, "onRetainCustomNonConfigurationInstance"); return super.onRetainCustomNonConfigurationInstance(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); Log.v(TAG, "onConfigurationChanged"); } /** * ? * * @param fullscreen */ @Override public void setFullScreen(boolean fullscreen) { if (fullscreen) { this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); } else { this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); } } @Override public boolean isFullScreen() { int flag = this.getWindow().getAttributes().flags; if ((flag & WindowManager.LayoutParams.FLAG_FULLSCREEN) == WindowManager.LayoutParams.FLAG_FULLSCREEN) { return true; } else { return false; } } @Override final public void onBackPressed() { Log.v(TAG, "onBackPressed "); if (null == stack || stack.size() == 0 || stack.peek() == null) { onBack(); return; } else { if (stack.peek().onBackPressed()) { return; } else { if (stack.size() == 1) { onBack(); return; } else { stack.popBackStack(); return; } } } } /** * ?back */ public void onBack() { Log.v(TAG, "onBack"); super.onBackPressed(); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { if (null == stack || stack.size() == 0 || stack.peek() == null) { return super.onKeyUp(keyCode, event); } else { if (stack.peek().onKeyUp(keyCode, event)) { return true; } else { return super.onKeyUp(keyCode, event); } } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (null == stack || stack.size() == 0 || stack.peek() == null) { return super.onKeyDown(keyCode, event); } else { if (stack.peek().onKeyDown(keyCode, event)) { return true; } else { return super.onKeyDown(keyCode, event); } } } @Override public void showSoftInput(EditText editText) { editText.requestFocus(); InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(editText, 0); editText.setText(null); } @Override public void hideSoftInput() { InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); if (this.getCurrentFocus() != null) { imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } @Override public Handler getHandler() { return handler; } protected void postRunnable(StaticInnerRunnable runnable) { if (handler != null && runnable != null) handler.post(runnable); } protected void handleMessage(Message msg) { } protected static class StaticInnerRunnable implements Runnable { @Override public void run() { } } final static class InternalHandler extends Handler { private final WeakReference<Context> mContext; public InternalHandler(Context context, Looper looper) { super(looper); mContext = new WeakReference<Context>(context); } public void handleMessage(Message msg) { Context context = mContext.get(); if (context != null) { ((BaseFragmentActivity) context).handleMessage(msg); } } } @ColorInt public int getThemeAttrColor(@AttrRes int colorAttr) { TypedArray array = this.obtainStyledAttributes(null, new int[] { colorAttr }); try { return array.getColor(0, 0); } finally { array.recycle(); } } }