Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 12728cf

Browse files
thatfiredevsamtstern
authored andcommitted
feat: Create Tasks Sample in Kotlin (firebase#26)
1 parent 029bc46 commit 12728cf

File tree

5 files changed

+189
-7
lines changed

5 files changed

+189
-7
lines changed

tasks/app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android'
23

34
android {
45
compileSdkVersion 27
@@ -25,4 +26,5 @@ dependencies {
2526

2627
implementation "com.google.firebase:firebase-auth:16.0.2"
2728
implementation "com.google.firebase:firebase-storage:16.0.1"
29+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
2830
}

tasks/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<category android:name="android.intent.category.LAUNCHER" />
1616
</intent-filter>
1717
</activity>
18+
<activity android:name=".KotlinMainActivity"/>
1819
</application>
1920

2021
</manifest>
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package com.google.firebase.quickstart.tasks
2+
3+
import android.os.Bundle
4+
import android.support.v7.app.AppCompatActivity
5+
import com.google.android.gms.tasks.OnCompleteListener
6+
import com.google.android.gms.tasks.Task
7+
import com.google.android.gms.tasks.Tasks
8+
import com.google.firebase.auth.AuthResult
9+
import com.google.firebase.auth.FirebaseAuth
10+
import com.google.firebase.auth.FirebaseAuthException
11+
import com.google.firebase.quickstart.tasks.interfaces.MainActivityInterface
12+
import java.util.concurrent.*
13+
14+
class KotlinMainActivity : AppCompatActivity(), MainActivityInterface {
15+
16+
// [START basic_sign_in_task]
17+
val task = FirebaseAuth.getInstance().signInAnonymously()
18+
// [END basic_sign_in_task]
19+
20+
override fun onCreate(savedInstanceState: Bundle?) {
21+
super.onCreate(savedInstanceState)
22+
setContentView(R.layout.activity_main)
23+
}
24+
25+
override fun basicTaskHandlers() {
26+
// [START success_listener]
27+
task.addOnSuccessListener { authResult ->
28+
// Task completed successfully
29+
// ...
30+
}
31+
// [END success_listener]
32+
33+
// [START failure_listener]
34+
task.addOnFailureListener { e ->
35+
// Task failed with an exception
36+
// ...
37+
}
38+
// [END failure_listener]
39+
40+
// [START completion_listener]
41+
task.addOnCompleteListener { task ->
42+
if (task.isSuccessful()) {
43+
// Task completed successfully
44+
val result = task.result
45+
} else {
46+
// Task failed with an exception
47+
val exception = task.exception
48+
}
49+
}
50+
// [END completion_listener]
51+
52+
// [START listener_try_catch]
53+
val signInTask = FirebaseAuth.getInstance().signInWithEmailAndPassword(
54+
"[email protected]", "mypassword1234")
55+
signInTask.addOnCompleteListener { task ->
56+
try {
57+
// Specific error information can be obtained by passing the expected
58+
// exception type into getResult(). In this case we expect a
59+
// FirebaseAuthException based on the documentation,
60+
val authResult = task.getResult(FirebaseAuthException::class.java)
61+
} catch (e: FirebaseAuthException) {
62+
// Task failed with FirebaseAuthException, which provides specific error
63+
// error information. such as the error code.
64+
val errorCode = e.errorCode
65+
}
66+
}
67+
// [END listener_try_catch]
68+
}
69+
70+
override fun taskOnExecutor() {
71+
// [START create_handler_and_executor]
72+
// Create a new ThreadPoolExecutor with 2 threads for each processor on the
73+
// device and a 60 second keep-alive time.
74+
val numCores = Runtime.getRuntime().availableProcessors()
75+
val executor = ThreadPoolExecutor(numCores * 2, numCores *2,
76+
60L, TimeUnit.SECONDS, LinkedBlockingQueue<Runnable>())
77+
// [END create_handler_and_executor]
78+
79+
// [START run_task_executor]
80+
task.addOnCompleteListener(executor, OnCompleteListener { task ->
81+
// ...
82+
})
83+
// [END run_task_executor]
84+
}
85+
86+
override fun activityScopedTask() {
87+
// [START activity_scoped]
88+
val activity = this
89+
task.addOnCompleteListener(activity, OnCompleteListener { task ->
90+
// ...
91+
})
92+
// [END activity_scoped]
93+
}
94+
95+
override
96+
// [START string_task_method]
97+
fun doSomething(authResult: AuthResult): Task<String> {
98+
// [START_EXCLUDE]
99+
return Tasks.forResult("Hello, World!")
100+
// [END_EXCLUDE]
101+
}
102+
// [END string_task_method]
103+
104+
override fun taskChaining() {
105+
// [START task_chaining]
106+
val signInTask = FirebaseAuth.getInstance().signInAnonymously()
107+
108+
signInTask.continueWithTask { task ->
109+
// Take the result from the first task and start the second one
110+
val result = task.result
111+
return@continueWithTask doSomething(result)
112+
}.addOnSuccessListener { s ->
113+
// Chain of tasks completed successfully, got result from last task.
114+
// ...
115+
}.addOnFailureListener { e ->
116+
// One of the tasks in the chain failed with an exception.
117+
// ...
118+
}
119+
// [END task_chaining]
120+
}
121+
122+
override fun blockingTask() {
123+
// [START blocking_task]
124+
try {
125+
// Block on a task and get the result synchronously. This is generally done
126+
// when executing a task inside a separately managed background thread. Doing this
127+
// on the main (UI) thread can cause your application to become unresponsive.
128+
val authResult = Tasks.await(task)
129+
} catch (e: ExecutionException) {
130+
// The Task failed, this is the same exception you'd get in a non-blocking
131+
// failure handler.
132+
// ...
133+
} catch (e: InterruptedException) {
134+
// An interrupt occurred while waiting for the task to complete.
135+
// ...
136+
}
137+
// [END blocking_task]
138+
139+
// [START blocking_task_timeout]
140+
try {
141+
// Block on the task for a maximum of 500 milliseconds, otherwise time out.
142+
val authResult = Tasks.await(task, 500, TimeUnit.MILLISECONDS)
143+
} catch (e: ExecutionException) {
144+
// ...
145+
} catch (e: InterruptedException) {
146+
// ...
147+
} catch (e: TimeoutException) {
148+
// Task timed out before it could complete.
149+
// ...
150+
}
151+
// [END blocking_task_timeout]
152+
}
153+
}

tasks/app/src/main/java/com/google/firebase/quickstart/tasks/MainActivity.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@
2929
import com.google.firebase.auth.AuthResult;
3030
import com.google.firebase.auth.FirebaseAuth;
3131
import com.google.firebase.auth.FirebaseAuthException;
32+
import com.google.firebase.quickstart.tasks.interfaces.MainActivityInterface;
3233

3334
import java.util.concurrent.ExecutionException;
3435
import java.util.concurrent.LinkedBlockingQueue;
3536
import java.util.concurrent.ThreadPoolExecutor;
3637
import java.util.concurrent.TimeUnit;
3738
import java.util.concurrent.TimeoutException;
3839

39-
public class MainActivity extends AppCompatActivity {
40+
public class MainActivity extends AppCompatActivity implements MainActivityInterface {
4041

4142
// [START basic_sign_in_task]
4243
Task<AuthResult> task = FirebaseAuth.getInstance().signInAnonymously();
@@ -48,7 +49,8 @@ protected void onCreate(Bundle savedInstanceState) {
4849
setContentView(R.layout.activity_main);
4950
}
5051

51-
private void basicTaskHandlers() {
52+
@Override
53+
public void basicTaskHandlers() {
5254
// [START success_listener]
5355
task.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
5456
@Override
@@ -105,7 +107,8 @@ public void onComplete(@NonNull Task<AuthResult> task) {
105107
// [END listener_try_catch]
106108
}
107109

108-
private void taskOnExecutor() {
110+
@Override
111+
public void taskOnExecutor() {
109112
// [START create_handler_and_executor]
110113
// Create a new ThreadPoolExecutor with 2 threads for each processor on the
111114
// device and a 60 second keep-alive time.
@@ -124,7 +127,8 @@ public void onComplete(@NonNull Task<AuthResult> task) {
124127
// [END run_task_executor]
125128
}
126129

127-
private void activityScopedTask() {
130+
@Override
131+
public void activityScopedTask() {
128132
// [START activity_scoped]
129133
Activity activity = MainActivity.this;
130134
task.addOnCompleteListener(activity, new OnCompleteListener<AuthResult>() {
@@ -136,15 +140,17 @@ public void onComplete(@NonNull Task<AuthResult> task) {
136140
// [END activity_scoped]
137141
}
138142

143+
@Override
139144
// [START string_task_method]
140-
private Task<String> doSomething(AuthResult authResult) {
145+
public Task<String> doSomething(AuthResult authResult) {
141146
// [START_EXCLUDE]
142147
return Tasks.forResult("Hello, World!");
143148
// [END_EXCLUDE]
144149
}
145150
// [END string_task_method]
146151

147-
private void taskChaining() {
152+
@Override
153+
public void taskChaining() {
148154
// [START task_chaining]
149155
Task<AuthResult> signInTask = FirebaseAuth.getInstance().signInAnonymously();
150156

@@ -171,7 +177,8 @@ public void onFailure(@NonNull Exception e) {
171177
// [END task_chaining]
172178
}
173179

174-
private void blockingTask() {
180+
@Override
181+
public void blockingTask() {
175182
// [START blocking_task]
176183
try {
177184
// Block on a task and get the result synchronously. This is generally done
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.google.firebase.quickstart.tasks.interfaces;
2+
3+
import com.google.android.gms.tasks.Task;
4+
import com.google.firebase.auth.AuthResult;
5+
6+
public interface MainActivityInterface {
7+
8+
void basicTaskHandlers();
9+
10+
void taskOnExecutor();
11+
12+
void activityScopedTask();
13+
14+
Task<String> doSomething(AuthResult authResult);
15+
16+
void taskChaining();
17+
18+
void blockingTask();
19+
}

0 commit comments

Comments
 (0)