Skip to content

Commit d625c60

Browse files
committed
Adding Otto and foreground service as an example of how to use Otto and background services.
Removing comments
1 parent 2240626 commit d625c60

35 files changed

+682
-2
lines changed

app/AndroidManifest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
</intent-filter>
3535
</activity>
3636

37+
<activity android:name=".ui.BootstrapTimerActivity"
38+
android:configChanges="orientation|keyboardHidden|screenSize" android:label="@string/app_name"
39+
android:launchMode="singleTop"/>
40+
41+
<service android:name=".core.TimerService" android:enabled="true" android:exported="false" />
3742

3843
<service
3944
android:name=".authenticator.AccountAuthenticatorService"

app/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@
8080
<version>0.8</version>
8181
<type>apklib</type>
8282
</dependency>
83+
<dependency>
84+
<groupId>com.squareup</groupId>
85+
<artifactId>otto</artifactId>
86+
<version>1.3.2</version>
87+
</dependency>
8388
<dependency>
8489
<groupId>junit</groupId>
8590
<artifactId>junit</artifactId>
Loading
Loading
1.05 KB
Loading
Loading
Loading
Loading
Loading
Loading
695 Bytes
Loading
905 Bytes
Loading
Loading
Loading
1.47 KB
Loading
Loading
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
4+
5+
<gradient
6+
android:angle="270"
7+
android:endColor="@color/button_background_pressed_end"
8+
android:startColor="@color/button_background_pressed_start" />
9+
10+
<corners android:radius="3dp" />
11+
12+
</shape>

app/res/drawable/button_background_states.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
<selector xmlns:android="http://schemas.android.com/apk/res/android">
44

5-
<item android:drawable="@drawable/button_background_enabled" android:state_enabled="true"/>
5+
<item android:drawable="@drawable/button_background_pressed" android:state_pressed="true" />
66
<item android:drawable="@drawable/button_background_disabled" android:state_enabled="false"/>
7+
<item android:drawable="@drawable/button_background_enabled" />
78

89
</selector>

app/res/layout/bootstrap_timer.xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:orientation="vertical"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent">
7+
8+
<TextView
9+
android:id="@+id/chronometer"
10+
android:layout_width="fill_parent"
11+
android:layout_height="wrap_content"
12+
android:text="00:00:00"
13+
style="@style/TimerChronometer"
14+
/>
15+
16+
<Button
17+
android:id="@+id/start"
18+
android:layout_width="fill_parent"
19+
android:layout_height="wrap_content"
20+
android:text="@string/start"
21+
style="@style/BootstrapButton"
22+
/>
23+
24+
<Button
25+
android:id="@+id/stop"
26+
android:layout_width="fill_parent"
27+
android:layout_height="wrap_content"
28+
android:text="@string/stop"
29+
android:visibility="gone"
30+
style="@style/BootstrapButton"
31+
/>
32+
33+
<Button
34+
android:id="@+id/pause"
35+
android:layout_width="fill_parent"
36+
android:layout_height="wrap_content"
37+
android:text="@string/pause"
38+
android:visibility="gone"
39+
style="@style/BootstrapButton"
40+
/>
41+
42+
<Button
43+
android:id="@+id/resume"
44+
android:layout_width="fill_parent"
45+
android:layout_height="wrap_content"
46+
android:text="@string/resume"
47+
android:visibility="gone"
48+
style="@style/BootstrapButton"
49+
/>
50+
51+
</LinearLayout>

app/res/menu/bootstrap.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
android:title="@string/refresh_action" >
1111
</item>
1212

13+
<item
14+
android:id="@+id/timer"
15+
android:icon="@drawable/ic_action_timer"
16+
android:showAsAction="always"
17+
android:title="@string/timer">
18+
</item>
19+
1320
<item
1421
android:id="@+id/logout"
1522
android:showAsAction="never"

app/res/values/colors.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
<color name="main_background_end">#2C2C2C</color>
2828
<color name="button_background_enabled_start">#1fb6ed</color>
2929
<color name="button_background_enabled_end">#1c9ac8</color>
30+
<color name="button_background_pressed_start">#f76500</color>
31+
<color name="button_background_pressed_end">#fb812c</color>
3032
<color name="edit_text_background_start">#EFF6FF</color>
3133
<color name="edit_text_background_end">#FBFDFF</color>
3234
<color name="button_background_disabled_start">#7b7b7b</color>

app/res/values/strings.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
3232
<string name="message_bad_credentials">Email Address or Password is incorrect</string>
3333
<string name="message_auth_failed">Please enter a valid password</string>
3434
<string name="message_auth_failed_new_account">Please enter a valid email address &amp; password</string>
35+
<string name="timer_running">Timer is running</string>
36+
<string name="timer_is_paused">Timer is paused</string>
37+
<string name="stop">Stop</string>
38+
<string name="start">Start</string>
39+
<string name="pause">Pause</string>
40+
<string name="resume">Resume</string>
41+
<string name="timer">Timer</string>
3542

3643

3744
</resources>

app/res/values/styles.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,19 @@
7474
<item name="android:background">@drawable/list_item_background</item>
7575
</style>
7676

77+
<style name="BootstrapButton">
78+
<item name="android:background">@drawable/button_background_states</item>
79+
<item name="android:layout_marginLeft">10dp</item>
80+
<item name="android:layout_marginRight">10dp</item>
81+
<item name="android:layout_marginTop">5dp</item>
82+
<item name="android:layout_marginBottom">5dp</item>
83+
<item name="android:textStyle">bold</item>
84+
</style>
85+
86+
<style name="TimerChronometer">
87+
<item name="android:gravity">center_horizontal</item>
88+
<item name="android:textSize">75sp</item>
89+
<item name="android:textColor">#464646</item>
90+
</style>
91+
7792
</resources>

app/src/main/java/com/donnfelker/android/bootstrap/BootstrapApplication.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
import android.content.Context;
1010

1111
import com.github.kevinsawicki.http.HttpRequest;
12+
import com.google.inject.Injector;
13+
import com.google.inject.Stage;
14+
15+
import roboguice.RoboGuice;
1216

1317
/**
1418
* Android Bootstrap application
@@ -34,6 +38,13 @@ public BootstrapApplication(final Context context) {
3438
attachBaseContext(context);
3539
}
3640

41+
@Override
42+
public void onCreate() {
43+
super.onCreate();
44+
45+
setApplicationInjector(this);
46+
}
47+
3748
/**
3849
* Create main application
3950
*
@@ -43,4 +54,15 @@ public BootstrapApplication(final Instrumentation instrumentation) {
4354
this();
4455
attachBaseContext(instrumentation.getTargetContext());
4556
}
57+
58+
/**
59+
* Sets the application injector. Using the {@link RoboGuice#newDefaultRoboModule} as well as a
60+
* custom binding module {@link BootstrapModule} to set up your application module
61+
* @param application
62+
* @return
63+
*/
64+
public static Injector setApplicationInjector(Application application) {
65+
return RoboGuice.setBaseApplicationInjector(application, Stage.DEVELOPMENT, RoboGuice.newDefaultRoboModule
66+
(application), new BootstrapModule());
67+
}
4668
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.donnfelker.android.bootstrap;
2+
3+
import com.google.inject.AbstractModule;
4+
import com.google.inject.Singleton;
5+
import com.squareup.otto.Bus;
6+
7+
/**
8+
* Module for setting up custom bindings in RoboGuice.
9+
*/
10+
public class BootstrapModule extends AbstractModule {
11+
12+
@Override
13+
protected void configure() {
14+
15+
// We want Otto to be bound as a singleton as one instance only needs
16+
// to be present in this app
17+
bind(Bus.class).in(Singleton.class);
18+
19+
}
20+
21+
}

app/src/main/java/com/donnfelker/android/bootstrap/core/Constants.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ private Intent() {}
9797

9898
}
9999

100+
public static class Notification{
101+
private Notification() {}
102+
103+
public static final int TIMER_NOTIFICATION_ID = 1000; // Why 1000? Why not? :)
104+
}
105+
100106
}
101107

102108

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.donnfelker.android.bootstrap.core;
2+
3+
/**
4+
* Marker class for Otto for a pause event for the timer.
5+
*/
6+
public class PauseTimerEvent {
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.donnfelker.android.bootstrap.core;
2+
3+
/**
4+
* Marker class for resuming a timer through Otto
5+
*/
6+
public class ResumeTimerEvent {
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.donnfelker.android.bootstrap.core;
2+
3+
/**
4+
* Marker class for the stop timer event in Otto.
5+
*/
6+
public class StopTimerEvent {
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.donnfelker.android.bootstrap.core;
2+
3+
public class TimerPausedEvent {
4+
5+
private boolean timerIsPaused;
6+
7+
public TimerPausedEvent(boolean timerIsPaused) {
8+
this.timerIsPaused = timerIsPaused;
9+
}
10+
11+
public boolean isTimerIsPaused() {
12+
return timerIsPaused;
13+
}
14+
}

0 commit comments

Comments
 (0)