Back to project page android-mvc-framework.
The source code is released under:
Apache License
If you think the Android project android-mvc-framework 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.android_mvc.framework.activities; /*w w w.ja v a 2 s .c om*/ import java.util.regex.Matcher; import java.util.regex.Pattern; import com.android_mvc.framework.common.BaseUtil; import com.android_mvc.framework.common.FWUtil; import com.android_mvc.framework.controller.action.ActionResult; import com.android_mvc.framework.controller.routing.Router; import com.android_mvc.framework.controller.validation.ValidationResult; import com.android_mvc.framework.task.AsyncTasksRunner; import com.android_mvc.framework.task.RunnerFollower; import com.android_mvc.framework.task.SequentialAsyncTask; import com.android_mvc.framework.ui.menu.OptionMenuBuilder; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; /** * Map?????Map????Activity?????????????????? * @author id:language_and_engineering * */ public class CommonActivityUtil<T extends IBaseActivity> { // Activity????????$???????????????????????? // NOTE: ????????????T?Activity ????????????????? // ????????????????????????????????????????????? private Activity activity; // ?????????????????????????Intent?????? private Bundle extras; private ValidationResult vres; private ActionResult ares; // ????????????????????? private OptionMenuBuilder optionMenuBuilder; // ????????????????????????????????????? private boolean menuBuiltFlag = false; // ---------------- Activity????????? ------------------ /** * Activity???onCreate???????????????????? */ public void onActivityCreated(T activity) { FWUtil.d("onActivityCreated??"); this.activity = (Activity)activity; // ??????????????????????????????????? carryDataFromPreviousPage(activity); // UI?????????????????????? if( activity.requireProcBeforeUI() ) { // ?????????????????????????????UI???? executeProcBeforeUIAndRender(activity); } else { // ?????????????????UI???? renderUI(activity); } FWUtil.d("onActivityCreated??"); } /** * ??????????Intent?????????????????????????????????????????????????????????? */ private void carryDataFromPreviousPage(T activity) { // Intent??????????? extras = ((Activity)activity).getIntent().getExtras(); // ???????????? if( extras == null ) return; // ???????????????????????? if( extras.containsKey(Router.EXTRA_KEY_VALIDATION_RESULT)) { vres = (ValidationResult) extras.getSerializable(Router.EXTRA_KEY_VALIDATION_RESULT); } // ??????????????????????? if( extras.containsKey(Router.EXTRA_KEY_ACTION_RESULT)) { ares = (ActionResult) extras.getSerializable(Router.EXTRA_KEY_ACTION_RESULT); } } /** * UI????????????????????????????????????????UI???????? */ private void executeProcBeforeUIAndRender(final T activity) { new AsyncTasksRunner( new SequentialAsyncTask[]{ new SequentialAsyncTask(){ @Override protected boolean main() { // ?????????? activity.procAsyncBeforeUI(); return CONTINUE_TASKS; } } }) .withSimpleDialog("????????????", (Activity)activity) .whenAllTasksCompleted(new RunnerFollower(){ @Override protected void exec() { // UI????????UI??? renderUI(activity); }}) .begin(); } /** * UI??? */ private void renderUI(T activity) { // XML??????????????????????? render_xml( (Activity) activity ); // UI?????? activity.defineContentView(); // ??????? this.optionMenuBuilder = activity.defineMenu(); // ?????????????????????????????????? activity.afterViewDefined(); } /** * ???????????????????????XML????????????????? */ private void render_xml( Activity activity ) { // NOTE: ??????????layout???????????????????????????? // ?????????????????????????????????????????????????????? // http://ameblo.jp/m-ext/entry-10872776112.html // ???????????????Activity????? String activity_class_name = activity.getClass().getSimpleName(); Pattern reg_pattern = Pattern.compile( "Activity$" ); Matcher reg_matcher = reg_pattern.matcher( activity_class_name ); String activity_basic_name = reg_matcher.replaceFirst(""); // ??????????????????????PascalCase??????????????snake_case???????? StringBuilder sb = new StringBuilder(); int class_name_length = activity_basic_name.length(); boolean previous_char_was_upper = false; // 1????????????????????????????????????? for( int i = 0; i < class_name_length; i ++ ) { Character c = activity_basic_name.charAt(i); // ?????? if( Character.isUpperCase(c)) { // ????????????????????????????????? if( ( i > 0 ) && ( ! previous_char_was_upper ) ) { sb.append("_"); } // ??????????? c = Character.toLowerCase(c); previous_char_was_upper = true; } else { previous_char_was_upper = false; } // ?? sb.append(c); } // ?????XML????????????????? String xml_base_name = sb.toString(); BaseUtil.d( "xml name is " + xml_base_name); // ??????????????????XML???????ID????? int xml_resource_id = activity .getResources() .getIdentifier( xml_base_name, "layout", activity.getPackageName() ); // ??????XML?????????????????????? if( xml_resource_id != 0 ) { activity.setContentView( xml_resource_id ); } else { // NOTE: ??????????????????XML?????????????????? BaseUtil.w("xml not found!"); } return; } // ---------------- ?????????????????? ------------------ /** * ?????????????????????????Intent????????????? * ??????????????????null?????? */ public Bundle extras() { return extras; // NOTE: AC?????????Bundle????????????????????????????????????? // TODO: RoutingTable??????????????Intent???????????????????????????????????????????? } /** * ??????????????????? * ??????????????????null?????? */ public ValidationResult getValidationResult() { return vres; } /** * ???????????????? * ??????????????????null?????? */ public ActionResult getActionResult() { return ares; } /** * ??????????????????????????????????? */ public boolean hasValidationResult() { return (vres != null); } /** * ???????????????????????????????? */ public boolean hasActionResult() { return (ares != null); } /** * ?????????????????????????????????????????? */ public boolean actionResultHasKey(String key) { if( ! hasActionResult() ) return false; return ( ares.get(key) != null ); } /** * ???????????????Intent???????????????????????????????????????? */ public boolean intentHasKey(String key) { if( extras() != null ) { return extras().containsKey( key ); } else { return false; } } /** * getText???????????????????????????? */ public String _(int target_string_id) { // ???????????? String target_string = activity .getResources() .getString( target_string_id ) ; return target_string; // @see http://d.hatena.ne.jp/language_and_engineering/20110815/p1 // TODO: Activity??????????????????? } // ---------------- ?????? ------------------ /** * ???????????????? */ public Menu renderOptionMenuAsDescribed(Menu menu) { // ????????????? if( ! menuBuiltFlag ) { // ???????????????????????????? if( optionMenuBuilder != null ) { menu = optionMenuBuilder.registerItemsInMenu(menu); } } // ??????????????????????? menuBuiltFlag = true; return menu; } /** * ???????????????????????????????????????? */ public void onOptionItemSelected(MenuItem item) { if( optionMenuBuilder != null ) { optionMenuBuilder.onItemSelected(item); } } }