Skip to content

Commit fbc5ebc

Browse files
author
Matt Monasch
committed
Updating hello-jnicallback to support large screen devices
Timer does not restart on configuration changes and utilizes a ViewModel to preserve state. Button added to start and stop. Small naming convention fix with stopTimer
1 parent 7a8ff4c commit fbc5ebc

File tree

5 files changed

+114
-28
lines changed

5 files changed

+114
-28
lines changed

hello-jniCallback/app/src/main/cpp/hello-jnicallback.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ Java_com_example_hellojnicallback_MainActivity_startTicks(JNIEnv *env, jobject i
284284
* for a clean shutdown. The caller is from onPause
285285
*/
286286
JNIEXPORT void JNICALL
287-
Java_com_example_hellojnicallback_MainActivity_StopTicks(JNIEnv *env, jobject instance) {
287+
Java_com_example_hellojnicallback_MainActivity_stopTicks(JNIEnv *env, jobject instance) {
288288
pthread_mutex_lock(&g_ctx.lock);
289289
g_ctx.done = 1;
290290
pthread_mutex_unlock(&g_ctx.lock);

hello-jniCallback/app/src/main/java/com/example/hellojnicallback/MainActivity.java

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,56 +17,98 @@
1717

1818
import androidx.annotation.Keep;
1919
import androidx.appcompat.app.AppCompatActivity;
20+
import androidx.lifecycle.ViewModelProvider;
21+
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory;
22+
2023
import android.os.Bundle;
24+
import android.view.View;
25+
import android.widget.Button;
2126
import android.widget.TextView;
2227

2328
public class MainActivity extends AppCompatActivity {
2429

25-
int hour = 0;
26-
int minute = 0;
27-
int second = 0;
30+
MainViewModel model;
2831
TextView tickView;
32+
TextView helloJniMsg;
33+
Button pauseBtn;
34+
2935
@Override
3036
protected void onCreate(Bundle savedInstanceState) {
3137
super.onCreate(savedInstanceState);
3238
setContentView(R.layout.activity_main);
33-
tickView = (TextView) findViewById(R.id.tickView);
39+
tickView = findViewById(R.id.tickView);
40+
helloJniMsg = findViewById(R.id.hellojniMsg);
41+
pauseBtn = findViewById(R.id.pauseBtn);
42+
43+
// Fetch the ViewModel, or have one instantiated
44+
model = new ViewModelProvider(this,
45+
AndroidViewModelFactory.getInstance(this.getApplication())
46+
).get(MainViewModel.class);
47+
48+
// if the timer has yet to be started, toggle it's state to trigger it's first run
49+
// otherwise, and only if it was previously running, start the jni thread for tick callbacks
50+
if(!model.started) {
51+
model.started = true;
52+
toggleTimerState();
53+
} else {
54+
if(model.running) {
55+
startTicks();
56+
}
57+
}
58+
59+
setText();
3460
}
61+
62+
/*
63+
* onDestroy gets called for configuration changes.
64+
* We make sure that the jni context cleans up so we don't lose track of threads.
65+
*/
3566
@Override
36-
public void onResume() {
37-
super.onResume();
38-
hour = minute = second = 0;
39-
((TextView)findViewById(R.id.hellojniMsg)).setText(stringFromJNI());
40-
startTicks();
67+
protected void onDestroy(){
68+
super.onDestroy();
69+
if(model.running) {
70+
stopTicks();
71+
}
4172
}
4273

43-
@Override
44-
public void onPause () {
45-
super.onPause();
46-
StopTicks();
74+
private void toggleTimerState() {
75+
model.running = !model.running;
76+
setText();
77+
if(model.running){
78+
model.resetTimer();
79+
startTicks();
80+
} else {
81+
stopTicks();
82+
}
83+
}
84+
85+
private void setText() {
86+
helloJniMsg.setText(stringFromJNI());
87+
tickView.setText(model.time());
88+
89+
if(model.running) {
90+
pauseBtn.setText(R.string.pause);
91+
} else {
92+
pauseBtn.setText(R.string.resume);
93+
}
94+
95+
96+
}
97+
98+
public void onPauseBtn(View v){
99+
toggleTimerState();
47100
}
48101

49102
/*
50103
* A function calling from JNI to update current timer
51104
*/
52105
@Keep
53106
private void updateTimer() {
54-
++second;
55-
if(second >= 60) {
56-
++minute;
57-
second -= 60;
58-
if(minute >= 60) {
59-
++hour;
60-
minute -= 60;
61-
}
62-
}
107+
model.updateTimer();
63108
runOnUiThread(new Runnable() {
64109
@Override
65110
public void run() {
66-
String ticks = "" + MainActivity.this.hour + ":" +
67-
MainActivity.this.minute + ":" +
68-
MainActivity.this.second;
69-
MainActivity.this.tickView.setText(ticks);
111+
MainActivity.this.tickView.setText(model.time());
70112
}
71113
});
72114
}
@@ -75,5 +117,5 @@ public void run() {
75117
}
76118
public native String stringFromJNI();
77119
public native void startTicks();
78-
public native void StopTicks();
120+
public native void stopTicks();
79121
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.example.hellojnicallback;
2+
3+
import androidx.lifecycle.ViewModel;
4+
5+
public class MainViewModel extends ViewModel {
6+
private int hour = 0;
7+
private int minute = 0;
8+
private int second = 0;
9+
public boolean started = false;
10+
public boolean running = false;
11+
12+
public void updateTimer() {
13+
++second;
14+
if(second >= 60) {
15+
++minute;
16+
second -= 60;
17+
if(minute >= 60) {
18+
++hour;
19+
minute -= 60;
20+
}
21+
}
22+
}
23+
24+
public void resetTimer() {
25+
hour = minute = second = 0;
26+
}
27+
28+
public String time() {
29+
return hour + ":" + minute + ":" + second;
30+
}
31+
}

hello-jniCallback/app/src/main/res/layout/activity_main.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,16 @@
4343
app:layout_constraintBottom_toBottomOf="@+id/activity_hello_jnicallback"
4444
tools:layout_constraintBottom_creator="0" />
4545

46+
<Button
47+
android:id="@+id/pauseBtn"
48+
android:layout_width="wrap_content"
49+
android:layout_height="48dp"
50+
android:layout_marginTop="32dp"
51+
android:onClick="onPauseBtn"
52+
android:text="Button"
53+
app:layout_constraintEnd_toEndOf="parent"
54+
app:layout_constraintStart_toStartOf="parent"
55+
app:layout_constraintTop_toBottomOf="@+id/tickView" />
56+
4657

4758
</androidx.constraintlayout.widget.ConstraintLayout>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
<resources>
22
<string name="app_name">Hello-jniCallback</string>
3+
<string name="pause">PAUSE</string>
4+
<string name="resume">RESUME</string>
35
</resources>

0 commit comments

Comments
 (0)