Android Open Source - Mamytas Timer Service Helper






From Project

Back to project page Mamytas.

License

The source code is released under:

GNU General Public License

If you think the Android project Mamytas listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package mn.aug.restfulandroid.activity;
//from  w  ww . java2  s.com
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.ResultReceiver;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Paul on 16/11/2014.
 */
public class TimerServiceHelper {

    public static String ACTION_REQUEST_RESULT = "REQUEST_RESULT_TIMER";
    public static String EXTRA_REQUEST_ID = "EXTRA_REQUEST_ID";
    public static String EXTRA_RESULT_CODE = "EXTRA_RESULT_CODE";

    public static final String START_CHRONO = "mn.aug.restfulandroid.activity.action.START";
    public static final String STOP_CHRONO = "mn.aug.restfulandroid.activity.action.STOP";
    public static final String GET_CHRONO = "mn.aug.restfulandroid.activity.action.GET";

    private static final String startChronoHashkey = "start_chrono";
    private static final String stopChronoHashkey = "stop_chrono";
    private static final String getChronoHashkey= "get_chrono";


    public static final String EXTRA_TASK_ID = "mn.aug.restfulandroid.activity.extra.TASK_ID";
    public static final String EXTRA_INIT_VALUE = "mn.aug.restfulandroid.activity.extra.INIT_VALUE";
    public static final String EXTRA_RESULT = "mn.aug.restfulandroid.activity.extra.RESULT";

    private static Object lock = new Object();

    private static TimerServiceHelper instance;

    //TODO: refactor the key
    private Map<String,Long> pendingRequests = new HashMap<String,Long>();
    private Context ctx;

    private TimerServiceHelper(Context ctx){
        this.ctx = ctx.getApplicationContext();
    }

    public static TimerServiceHelper getInstance(Context ctx){
        synchronized (lock) {
            if(instance == null){
                instance = new TimerServiceHelper(ctx);
            }
        }

        return instance;
    }


    /**
     * Starts this service to perform action with the given parameters. If
     * the service is already performing a task this action will be queued.
     *
     * @see android.app.IntentService
     */
    public void startChrono(Context context, Long task_id, Long initial_value) {

        long requestId = generateRequestID();
        pendingRequests.put(startChronoHashkey, requestId);

        ResultReceiver serviceCallback = new ResultReceiver(null){
            @Override
            protected void onReceiveResult(int resultCode, Bundle resultData) {
                handleResponse(resultCode, resultData, startChronoHashkey);
            }
        };

        Intent intent = new Intent(context, TimerService.class);
        intent.setAction(START_CHRONO);
        intent.putExtra(EXTRA_TASK_ID, task_id);
        intent.putExtra(EXTRA_INIT_VALUE, initial_value);
        intent.putExtra(TimerService.SERVICE_CALLBACK, serviceCallback);
        intent.putExtra(EXTRA_REQUEST_ID, requestId);
        context.startService(intent);

    }

    /**
     * Starts this service to perform action with the given parameters. If
     * the service is already performing a task this action will be queued.
     *
     * @see android.app.IntentService
     */
    public void stopChrono(Context context, Long task_id) {


        long requestId = generateRequestID();
        pendingRequests.put(stopChronoHashkey, requestId);

        ResultReceiver serviceCallback = new ResultReceiver(null){
            @Override
            protected void onReceiveResult(int resultCode, Bundle resultData) {
                handleResponse(resultCode, resultData, stopChronoHashkey);
            }
        };



        Intent intent = new Intent(context, TimerService.class);
        intent.setAction(STOP_CHRONO);
        intent.putExtra(EXTRA_TASK_ID, task_id);
        intent.putExtra(TimerService.SERVICE_CALLBACK, serviceCallback);
        intent.putExtra(EXTRA_REQUEST_ID, requestId);
        context.startService(intent);
    }

    public long getChrono(Context context, Long task_id) {

        long requestId = generateRequestID();
        pendingRequests.put(getChronoHashkey, requestId);

        ResultReceiver serviceCallback = new ResultReceiver(null){
            @Override
            protected void onReceiveResult(int resultCode, Bundle resultData) {
                handleResponse(resultCode, resultData, getChronoHashkey);
            }
        };


        Intent intent = new Intent(context, TimerService.class);
        intent.setAction(GET_CHRONO);
        intent.putExtra(EXTRA_TASK_ID, task_id);
        intent.putExtra(TimerService.SERVICE_CALLBACK, serviceCallback);
        intent.putExtra(EXTRA_REQUEST_ID, requestId);
        context.startService(intent);
        return requestId;
    }


    private long generateRequestID() {
        long requestId = UUID.randomUUID().getLeastSignificantBits();
        return requestId;
    }

    public boolean isRequestPending(long requestId){
        return this.pendingRequests.containsValue(requestId);
    }


    private void handleResponse(int resultCode, Bundle resultData, String hashKey){

        Intent origIntent = (Intent)resultData.getParcelable(TimerService.ORIGINAL_INTENT_EXTRA);
         if(origIntent != null){
            long requestId = origIntent.getLongExtra(EXTRA_REQUEST_ID, 0);
            long result = origIntent.getLongExtra(EXTRA_RESULT, 0);

            pendingRequests.remove(hashKey);

            Intent resultBroadcast = new Intent(ACTION_REQUEST_RESULT);
            resultBroadcast.putExtra(EXTRA_REQUEST_ID, requestId);
            resultBroadcast.putExtra(EXTRA_RESULT_CODE, resultCode);
            resultBroadcast.putExtra(EXTRA_RESULT,result);

            ctx.sendBroadcast(resultBroadcast);
        }
    }






}




Java Source Code List

mn.aug.restfulandroid.activity.AboutActivity.java
mn.aug.restfulandroid.activity.LoginActivity.java
mn.aug.restfulandroid.activity.ProjectEditor.java
mn.aug.restfulandroid.activity.ProjectsActivity.java
mn.aug.restfulandroid.activity.ProjectsArrayAdapter.java
mn.aug.restfulandroid.activity.TaskActivity.java
mn.aug.restfulandroid.activity.TaskEditor.java
mn.aug.restfulandroid.activity.TasksActivity.java
mn.aug.restfulandroid.activity.TasksArrayAdapter.java
mn.aug.restfulandroid.activity.TimerServiceHelper.java
mn.aug.restfulandroid.activity.TimerService.java
mn.aug.restfulandroid.activity.TimersArrayAdapter.java
mn.aug.restfulandroid.activity.base.RESTfulActivity.java
mn.aug.restfulandroid.activity.base.RESTfulListActivity.java
mn.aug.restfulandroid.activity.base.UndoBarController.java
mn.aug.restfulandroid.provider.CommentsDBAccess.java
mn.aug.restfulandroid.provider.ListsDBAccess.java
mn.aug.restfulandroid.provider.OwnershipDBAccess.java
mn.aug.restfulandroid.provider.ProviderDbHelper.java
mn.aug.restfulandroid.provider.RemindersDBAccess.java
mn.aug.restfulandroid.provider.TasksDBAccess.java
mn.aug.restfulandroid.provider.UsersDBAccess.java
mn.aug.restfulandroid.rest.AbstractRestMethod.java
mn.aug.restfulandroid.rest.DeleteListRestMethod.java
mn.aug.restfulandroid.rest.DeleteTaskRestMethod.java
mn.aug.restfulandroid.rest.GetListsRestMethod.java
mn.aug.restfulandroid.rest.GetTasksRestMethod.java
mn.aug.restfulandroid.rest.GetTimersRestMethod.java
mn.aug.restfulandroid.rest.LoginRestMethod.java
mn.aug.restfulandroid.rest.PostListRestMethod.java
mn.aug.restfulandroid.rest.PostTaskRestMethod.java
mn.aug.restfulandroid.rest.PostTimerRestMethod.java
mn.aug.restfulandroid.rest.PutListRestMethod.java
mn.aug.restfulandroid.rest.PutTaskRestMethod.java
mn.aug.restfulandroid.rest.PutTimerRestMethod.java
mn.aug.restfulandroid.rest.Request.java
mn.aug.restfulandroid.rest.Response.java
mn.aug.restfulandroid.rest.RestClient.java
mn.aug.restfulandroid.rest.RestMethodFactory.java
mn.aug.restfulandroid.rest.RestMethodResult.java
mn.aug.restfulandroid.rest.RestMethod.java
mn.aug.restfulandroid.rest.ShareListRestMethod.java
mn.aug.restfulandroid.rest.resource.Comment.java
mn.aug.restfulandroid.rest.resource.Lists.java
mn.aug.restfulandroid.rest.resource.Listw.java
mn.aug.restfulandroid.rest.resource.Login.java
mn.aug.restfulandroid.rest.resource.Reminder.java
mn.aug.restfulandroid.rest.resource.Resource.java
mn.aug.restfulandroid.rest.resource.TaskList.java
mn.aug.restfulandroid.rest.resource.Task.java
mn.aug.restfulandroid.rest.resource.Tasks.java
mn.aug.restfulandroid.rest.resource.Timer.java
mn.aug.restfulandroid.rest.resource.Timers.java
mn.aug.restfulandroid.security.AuthorizationManager.java
mn.aug.restfulandroid.security.RequestSigner.java
mn.aug.restfulandroid.service.ListProcessor.java
mn.aug.restfulandroid.service.ListsProcessor.java
mn.aug.restfulandroid.service.LoginProcessor.java
mn.aug.restfulandroid.service.ProcessorCallback.java
mn.aug.restfulandroid.service.ShareProcessor.java
mn.aug.restfulandroid.service.TaskProcessor.java
mn.aug.restfulandroid.service.TasksProcessor.java
mn.aug.restfulandroid.service.TimersProcessor.java
mn.aug.restfulandroid.service.WunderlistServiceHelper.java
mn.aug.restfulandroid.service.WunderlistService.java
mn.aug.restfulandroid.util.DateHelper.java
mn.aug.restfulandroid.util.DatePickerFragment.java
mn.aug.restfulandroid.util.Logger.java
mn.aug.restfulandroid.util.TimePickerFragment.java