Back to project page split.
The source code is released under:
MIT License
If you think the Android project split 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.devfestco.split; //from ww w. j a v a 2 s. com import android.app.Activity; import android.app.AlarmManager; import android.app.Notification; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.BitmapFactory; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationManagerCompat; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import com.devfestco.split.Models.IterationListItem; import com.devfestco.split.Services.TimerService; import com.devfestco.split.Utils.TimeUtil; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.wearable.Wearable; import java.util.ArrayList; public class IterationActivity extends Activity implements AdapterView.OnItemClickListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { public static final String EXTRA_DURATION_SECONDS = "extra_duration_seconds"; public static final String ACTION_START_REST_TIMER = "start_rest_timer"; public static final String ACTION_REMOVE_TIMER = "action_remove_timer"; public static final String ACTION_SHOW_ALARM = "action_show_alarm"; public static final String SAVED_STATE_SELECTED_DURATION = "saved_state_selected_duration"; private static final int MAX_ITERATION_SECONDS = 1200; private ArrayList<IterationListItem> _iterationTimes = new ArrayList<IterationListItem>(); private GoogleApiClient _googleApiClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); if (getIntent().getIntExtra(EXTRA_DURATION_SECONDS, 0) > 0 && getIntent().getIntExtra(EXTRA_DURATION_SECONDS, 0) <= MAX_ITERATION_SECONDS) { setupTimer(getIntent().getIntExtra( EXTRA_DURATION_SECONDS, 0 ) * 1000, 1); finish(); return; } if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) == Intent.FLAG_ACTIVITY_NEW_TASK) { pref = PreferenceManager.getDefaultSharedPreferences(this); long duration = pref.getLong(SAVED_STATE_SELECTED_DURATION, 0); if (duration != 0) { setupTimer(duration, 1); finish(); return; } } setupIterationArray(); setContentView(R.layout.act_iteration); initList(); initGoogleApiClient(); } private void setupIterationArray() { int[] minutes = getResources().getIntArray(R.array.interation_minutes); for (final int minute : minutes) { IterationListItem item = new IterationListItem(getResources().getQuantityString(R.plurals.label_minutes, minute, minute), minute * 60 * 1000); _iterationTimes.add(item); } } private void initList() { final ListView listView = (ListView) findViewById(R.id.list_view); listView.setAdapter(new ArrayAdapter<IterationListItem>(this, R.layout.li_simple, _iterationTimes)); listView.setOnItemClickListener(this); } private void initGoogleApiClient() { _googleApiClient = new GoogleApiClient.Builder(this) .addApi(Wearable.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); } /** * Sets up the timer * @param duration * @param type 1 for work timer, 2 for rest timer */ private void setupTimer(long duration, int type) { NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.cancel(1); switch (type) { case 1: notificationManager.notify(1, buildWorkNotification(duration)); break; case 2: notificationManager.notify(2, buildRestNotification(duration)); break; default: break; } registerWorkAlarmManager(duration); finish(); } private Notification buildWorkNotification(long duration) { Intent removeIntent = new Intent(ACTION_SHOW_ALARM, null, this, TimerService.class); PendingIntent pendingRemoveIntent = PendingIntent.getService(this, 0, removeIntent, PendingIntent.FLAG_UPDATE_CURRENT); return new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Split Run") .setContentText(TimeUtil.getTimeString(duration)) .setUsesChronometer(true) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_large)) .setWhen(System.currentTimeMillis() + duration) .addAction(R.drawable.ic_action_cancel, "Remove Timer", pendingRemoveIntent) .setDeleteIntent(pendingRemoveIntent) .setLocalOnly(true) .build(); } private Notification buildRestNotification(long restDuration) { Intent remoteIntent = new Intent(ACTION_REMOVE_TIMER, null, this, TimerService.class); PendingIntent pendingRemoveIntent = PendingIntent.getService(this, 0, remoteIntent, PendingIntent.FLAG_UPDATE_CURRENT); return new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Split Rest") .setContentText(TimeUtil.getTimeString(restDuration)) .setUsesChronometer(true) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_large)) .setWhen(System.currentTimeMillis() + restDuration) .addAction(R.drawable.ic_action_cancel, "Remove Timer", pendingRemoveIntent) .setDeleteIntent(pendingRemoveIntent) .setLocalOnly(true) .build(); } private void registerWorkAlarmManager(long duration) { AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(ACTION_SHOW_ALARM, null, this, TimerService.class); PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); long time = System.currentTimeMillis() + duration; alarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent); } @Override public void onConnected(Bundle bundle) { } @Override protected void onStart() { super.onStart(); if (_googleApiClient != null) { _googleApiClient.connect(); } } @Override protected void onStop() { super.onStop(); if(_googleApiClient != null && _googleApiClient.isConnected()) { _googleApiClient.disconnect(); } } @Override protected void onDestroy() { super.onDestroy(); } @Override public void onConnectionSuspended(int i) { } @Override public void onConnectionFailed(ConnectionResult connectionResult) { } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences( this ); SharedPreferences.Editor editor = pref.edit(); editor.putLong(SAVED_STATE_SELECTED_DURATION, _iterationTimes.get(position).getDuration()); editor.commit(); setupTimer(_iterationTimes.get(position).getDuration(), 1); } }