Android Open Source - Mamytas Timer Service






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;
//w w  w . j a va  2  s.  c  om
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.os.SystemClock;

import java.util.HashMap;

/**
 * An {@link IntentService} subclass for handling asynchronous task requests in
 * a service on a separate handler thread.
 * <p/>
 * TODO: Customize class - update intent actions, extra parameters and static
 * helper methods.
 */
public class TimerService extends IntentService {


    public static final String SERVICE_CALLBACK = "wunderlist.SERVICE_CALLBACK";

    public static final String ORIGINAL_INTENT_EXTRA = "wunderlist.ORIGINAL_INTENT_EXTRA";

    private static final int REQUEST_INVALID = -1;


    private ResultReceiver mCallback;

    private Intent mOriginalRequestIntent;
    private static HashMap<Long,Time_storage> chronos = new HashMap<Long,Time_storage>();


    public TimerService() {
        super("TimerService");
    }

    @Override
    protected void onHandleIntent(Intent requestIntent) {

        mCallback = requestIntent.getParcelableExtra(SERVICE_CALLBACK);

        mOriginalRequestIntent = requestIntent;

        String action = mOriginalRequestIntent.getAction();

        if (action.equals(TimerServiceHelper.START_CHRONO)) {
            long task_id = mOriginalRequestIntent.getLongExtra(TimerServiceHelper.EXTRA_TASK_ID, 0L);
            long initial_timer = mOriginalRequestIntent.getLongExtra(TimerServiceHelper.EXTRA_INIT_VALUE, 0L);

            if (chronos.containsKey(task_id)) {
                chronos.remove(task_id);
            }
                Time_storage storage=new Time_storage(initial_timer, SystemClock.uptimeMillis());
                chronos.put(task_id,storage);

        } else if (action.equals(TimerServiceHelper.STOP_CHRONO)) {
            long task_id = mOriginalRequestIntent.getLongExtra(TimerServiceHelper.EXTRA_TASK_ID, 0L);
            if (chronos.containsKey(task_id)) {
                chronos.remove(task_id);
               }

        } else if (action.equals(TimerServiceHelper.GET_CHRONO)) {
            long task_id = mOriginalRequestIntent.getLongExtra(TimerServiceHelper.EXTRA_TASK_ID, 0L);
            if (chronos.containsKey(task_id)) {
                Time_storage storage = chronos.get(task_id);
                long initial_timer = storage.getChrono_init();
                long start_time = storage.getStart_time();
                long timeInMilliseconds = SystemClock.uptimeMillis() - start_time;
                long updatedTime = initial_timer + timeInMilliseconds;
                mCallback.send(1, getOriginalIntentBundleWithValue(updatedTime));
            }
             mCallback.send(0, getOriginalIntentBundle());
        }


    }




    protected Bundle getOriginalIntentBundle() {
        Bundle originalRequest = new Bundle();
        originalRequest.putParcelable(ORIGINAL_INTENT_EXTRA, mOriginalRequestIntent);
        return originalRequest;
    }

    protected Bundle getOriginalIntentBundleWithValue(Long chrono_ms) {
        Bundle originalRequest = new Bundle();
        mOriginalRequestIntent.putExtra(TimerServiceHelper.EXTRA_RESULT, chrono_ms);
        originalRequest.putParcelable(ORIGINAL_INTENT_EXTRA, mOriginalRequestIntent);
        return originalRequest;
    }



    private class Time_storage {
        private Long start_time;
        private Long chrono_init;

        private Time_storage(Long chrono_init, Long start_time) {
            this.chrono_init = chrono_init;
            this.start_time = start_time;
        }

        public Long getStart_time() {
            return start_time;
        }

        public void setStart_time(Long start_time) {
            this.start_time = start_time;
        }

        public Long getChrono_init() {
            return chrono_init;
        }

        public void setChrono_init(Long chrono_init) {
            this.chrono_init = chrono_init;
        }
    }


}




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