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+ }
0 commit comments