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.task; /*w ww . java 2 s .c o m*/ import java.util.HashMap; import java.util.Iterator; import java.util.Set; import android.app.ProgressDialog; import android.content.Context; import com.android_mvc.framework.annotations.SuppressDebugLog; import com.android_mvc.framework.common.FWUtil; /** * ?????????????????????????????????????? * * ???????????????UI???????????????????????????????????????????????????????????????? * ????UI????????????AsyncTasksRunner??????????????????????? * whenAllTasksCompleted() ????????????????????????????????????????? * @author id:language_and_engineering */ @SuppressDebugLog public class AsyncTasksRunner { // ???????????????????????? private SequentialAsyncTask[] tasks; // ???????????????????????????? private int executing_task_cursor = 0; // ??????????????????????? private HashMap<String, Object> data_from_all_tasks = new HashMap<String, Object>(); // ?????????? private boolean requireDialogFlag = false; // ????????????????? private String dialogMessage = null; // ????? private ProgressDialog dialog = null; // ??????????????? private Context context; // ????????????????? private RunnerFollower follower; /** * ???? * @param tasks ?????????????????????????????? */ public AsyncTasksRunner( SequentialAsyncTask[] tasks ) { this.tasks = tasks; // TODO: ????????????Context??????????????????????????????????????????????? } // ---------------- ???????? ----------------- /** * ????????? */ public void begin() { FWUtil.d("begin()??????????????????"); if( tasks.length > 0 ) { FWUtil.d("???????????????????????????"); if( requireDialogFlag ) { // ??????? dialog = new ProgressDialog( context ); dialog.setMessage( dialogMessage ); dialog.show(); } executeCurrentTask(); } FWUtil.d("begin()?????????????"); } /** * ????????????? */ private void executeCurrentTask() { FWUtil.d("????????????????????????????????" + executing_task_cursor); getCurrentTask().kickByRunner( this ); } /** * ?????????? */ private void executeNextTask() { executing_task_cursor ++; executeCurrentTask(); } // ---------------- ???????? ----------------- /** * ?????????????????????????????????????? */ public void onCurrentTaskFinished() { // ???????????????? mergeDataFromTask( getCurrentTask() ); if( mustMoveToNextTask() ) { FWUtil.d("????????????????????"); // ???????????? executeNextTask(); } else { FWUtil.d("?????????????????????????"); onAllTasksFinished(); } } /** * ??????????????????????????????? */ private void onAllTasksFinished() { // ??????????? if( requireDialogFlag && ( dialog != null )) { // ?????????? dialog.dismiss(); FWUtil.d("????????????????????"); } // ???????????? if( this.follower != null) { // NOTE: ???????????UI????????????????????????????? // ???AsyncTask???????????????????????? FWUtil.d("????????????????????????????"); follower.exec(); FWUtil.d("?????????????????????"); } FWUtil.d("?????????????????????????"); } /** * ?????????????????????????? */ public AsyncTasksRunner whenAllTasksCompleted(RunnerFollower follower) { this.follower = follower; return this; } // ---------------- ??????????????? ----------------- /** * ???????????????????????? */ private SequentialAsyncTask getCurrentTask() { return tasks[ executing_task_cursor ]; } /** * ?????????????????????????????????????????? */ private boolean isProccessingLastTask() { return ( tasks.length == ( executing_task_cursor + 1 ) ); } /** * ?????????????????????????? */ private boolean mustMoveToNextTask() { // ??????????????????????????????????OK??????????? return ( ( ! isProccessingLastTask() ) && ( getCurrentTask().tasksContinuable() ) ); } /** * ??????????????????? * @param task ???????????????????????? */ private void mergeDataFromTask( SequentialAsyncTask task ) { // HashMap???????????????????????????????? HashMap<String, Object> data_from_current_task = task.getStoredObjects(); Set<String> keySet = data_from_current_task.keySet(); Iterator<String> keyIterator = keySet.iterator(); // ??????????????????????????????????????????? while( keyIterator.hasNext() ) { // ???????????? String key = (String)keyIterator.next(); Object value = data_from_current_task.get( key ); // ????????? data_from_all_tasks.put( key, value ); FWUtil.d("???????????" + key + "???????????????????????"); } } /** * ???????????????????????????????????????????????????????? */ public Object getDataByKey( String key ) { return data_from_all_tasks.get( key ); } // -------------- ????? -------------- /** * ????????????????????????????????????????????????? * @param string ?????????????? */ public AsyncTasksRunner withSimpleDialog(String s, Context context ) { this.context = context; setRequireDialogFlag(true); setDialogMessage(s); return this; } private void setDialogMessage(String s) { dialogMessage = s; } private void setRequireDialogFlag(boolean b) { requireDialogFlag = b; } }