I have to migrate from the deprecated synchronous api FirebaseInstanceId.getInstance().getId(); to the new async firebase api:
FirebaseInstallations.getInstance().getId().addOnCompleteListener( task -> {
if (!task.isSuccessful()) {
Log.w(TAG, "getInstanceId failed", task.getException());
return;
}
// Get new Instance ID
String id = task.getResult();
});
My problem is that in my old project, in many different point of my code a method like the following one has being called, and all the called expecting a response synchrounus in-line without callback:
public static String getDeviceId() {
if (deviceId == null) {
initDeviceId();
}
return deviceId;
}
private static void initDeviceId() {
deviceId = FirebaseInstanceId.getInstance().getId();
}
How can migrate the code without rewriting all the project ?
I have thought to to edit the above methods in this way:
public static String getDeviceId() {
if (deviceId == null) {
deviceId = workerThread.submit(initDeviceId()).get()
}
return deviceId;
}
private fun initDeviceId():Callable<String?>{
return Callable {
val latch = CountDownLatch(1)
var result:String ?= null
FirebaseInstallations.getInstance().id.addOnCompleteListener{
task -> result = task.result
latch.countDown()
}
latch.await()
result
}
}
but in this way I risk to block mainthread.
Related
I want to await the other processes until "Getting Username from firestore and putting it into postMap" How can I make them wait? Because if they don't wait username can not uploading to firestore and that cause some problems. I know I can use "Async & Await" method but how? (You can look at the comment lines that I created and see which processes are happening there.)
if(selectedPicture != null){
imageReference.putFile(selectedPicture!!).addOnSuccessListener {
val uploadPictureReference = storage.reference.child("images").child(imageName)
uploadPictureReference.downloadUrl.addOnSuccessListener {
val downloadUrl = it.toString()
if(auth.currentUser != null){
val postMap = hashMapOf<String,Any>()
postMap.put("downloadUrl",downloadUrl)
postMap.put("userEmail",auth.currentUser!!.email!!)
postMap.put("comment",binding.uploadCommentText.text.toString())
postMap.put("date",Timestamp.now())
//Get Username from firestore and put it into postMap
db.collection("UserDetails").addSnapshotListener { value, error ->
if(error!=null){
Toast.makeText(this,error.localizedMessage,Toast.LENGTH_LONG).show()
}else{
if(value!=null){
if(!value.isEmpty){
val documents = value.documents
for (document in documents){
val username = document.get("username")as String
//Put username into postMap
postMap.put("username",username) as String
}
}
}
}
}
//upload postmap to firestore
firestore.collection("Posts").add(postMap).addOnSuccessListener {
finish()
}.addOnFailureListener{
Toast.makeText(this,it.localizedMessage,Toast.LENGTH_LONG).show()
}
}
}
}.addOnFailureListener{
Toast.makeText(this,it.localizedMessage,Toast.LENGTH_LONG).show()
}
}
I don't know 100 % what you are trying to achieve, but by adding the firebase-ktx library, you can use .await() to get your values inside a coroutine.
// Returns true when everything was successful, or false if not
suspend fun getUserNameAndPutInPostMap(selectedPicture: File?): Boolean {
try {
if (selectedPicture == null || auth.currentUser == null) return
imageReference.putFile(selectedPicture!!).await()
val downloadUrl = storage.reference.child("images").child(imageName).downloadUrl.await().toString()
val userName = db.collection("UserDetails").get("username").await().toString()
val postMap = hashMapOf<String,Any>().apply {
put("downloadUrl", downloadUrl)
put("userEmail", auth.currentUser!!.email!!)
put("comment",binding.uploadCommentText.text.toString())
put("date",Timestamp.now())
put("username",username)
}
firestore.collection("Posts").add(postMap).await()
} catch (e: Exception) {
return false
}
}
The call to firestore.collection("Posts").add(postMap) will need to be inside the addSnapshotListener callback, right after you populate the postMap with postMap.put("username",username) as String.
db.collection("UserDetails").addSnapshotListener { value, error ->
if(error!=null){
Toast.makeText(this,error.localizedMessage,Toast.LENGTH_LONG).show()
}else{
if(value!=null){
if(!value.isEmpty){
val documents = value.documents
for (document in documents){
val username = document.get("username")as String
//Put username into postMap
postMap.put("username",username) as String
//upload postmap to firestore
firestore.collection("Posts").add(postMap).addOnSuccessListener {
finish()
}.addOnFailureListener{
Toast.makeText(this,it.localizedMessage,Toast.LENGTH_LONG).show()
}
}
}
}
}
}
I also recommend converting the onSnapshot to a get().addOnCompleteListener( call, because I'm pretty sure only mean to read the user data once.
I'm developing a feature for my app, where one user can send a notification to another user using cloud functions. My functions and my notifications work as expected, but I'm not able to handle errors in a proper way, because I always get "INTERNAL" as the error on my Android code.
Here is my code for Android:
public static Task<String> callFirebaseFunction(HashMap<String, Object> data, String funcion){
FirebaseFunctions mFunctions = FirebaseFunctions.getInstance();
return mFunctions
.getHttpsCallable(funcion)
.call(data)
.continueWith(new Continuation<HttpsCallableResult, String>() {
#Override
public String then(#NonNull Task<HttpsCallableResult> task) throws Exception {
return (String) task.getResult().getData();
}
});
}
Here is where I call callFirebaseFunction
Utilities.callFirebaseFunction(dataNoty, nombreFuncion)
.addOnCompleteListener(new OnCompleteListener<String>() {
#Override
public void onComplete(#NonNull Task<String> task) {
if ( !task.isSuccessful() ){
Exception e = task.getException();
if (e instanceof FirebaseFunctionsException) {
FirebaseFunctionsException ffe = (FirebaseFunctionsException) e;
FirebaseFunctionsException.Code code = ffe.getCode(); // It's always INTERNAL
Object details = ffe.getMessage(); // It's always INTERNAL
} else {
// handle error
}
} else {
// success
}
}
});
And here is my code for my cloud function
exports.helloWorld = functions.https.onRequest((req, res) => {
let data = req.body.data;
let id = data['id'].toString();
db.collection('users').doc(id).get()
.then((snapshot) => {
let infoUser = snapshot.data();
// Verifies data
if ( typeof infoUser !== "undefined" && Object.keys(infoUser).length > 0 ){
// Some code
} else {
throw new functions.https.HttpsError(
'invalid-argument', // code
'Error', // message
'User ' + id + ' not found' // details
);
}
}).then( () => {
console.log('Success');
return res.status(200).send("success");
}).catch( error => {
res.status(500).send(error.details);
return new functions.https.HttpsError(error.code, error.details);
});
});
I've tried different versions of code in my catch segment, for example:
.catch( error => {
res.status(500).send('Error');
throw new functions.https.HttpsError('invalid-argument', 'Error');
});
I don't know how am I supposed to make my catch segment, and I really need to get the error I throw in node (not just "INTERNAL").
Callable functions require the following format:
exports.yourFunction = functions.https.onCall((data, context) => {
// ...
});
It is not the same thing as an HTTP triggered Cloud Function, which is what you're using. You can still access it using an HTTP request from your Android app, but if you want to use mFunctions.getHttpsCallable(function).call(data), you will need to use a Callable Cloud Function, which I linked above.
If I understand well, once ARCore 1.0 will be released on Google Play, it will be necessary to install it on the device in order to be able to run an ARCore app.
How to check if ARCore lib/apk is installed on device ?
Should be sufficient to do something like:
try {
arCoreSession = Session(this)
val config = Config(arCoreSession)
if (!arCoreSession.isSupported(config)) {
Logger.d("ARCore not installed")
} else {
arCoreSession.configure(config)
}
} catch (ex: Throwable) {
Logger.d("ARCore not installed")
}
This is what I'm using here for one my apps and works fine on devices with or wothout ARCore.
According to ARCore documentation 1.4.0, if optional it is important check its availability recursively and then install it:
void maybeEnableArButton() {
// Likely called from Activity.onCreate() of an activity with AR buttons.
ArCoreApk.Availability availability = ArCoreApk.getInstance().checkAvailability(this);
if (availability.isTransient()) {
// re-query at 5Hz while we check compatibility.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
maybeEnableArButton();
}
}, 200);
}
if (availability.isSupported()) {
mArButton.setVisibility(View.VISIBLE);
mArButton.setEnabled(true);
// indicator on the button.
} else { // unsupported or unknown
mArButton.setVisibility(View.INVISIBLE);
mArButton.setEnabled(false);
}
}
If already supported just check if ARCore is installed:
// Set to true ensures requestInstall() triggers installation if necessary.
private boolean mUserRequestedInstall = true;
// in onResume:
try {
if (mSession == null) {
switch (ArCoreApk.getInstance().requestInstall(this, mUserRequestedInstall)) {
case INSTALLED:
mSession = new Session(this);
// Success.
break;
case INSTALL_REQUESTED:
// Ensures next invocation of requestInstall() will either return
// INSTALLED or throw an exception.
mUserRequestedInstall = false;
return;
}
}
} catch (UnavailableUserDeclinedInstallationException e) {
// Display an appropriate message to the user and return gracefully.
return;
} catch (...) { // current catch statements
...
return; // mSession is still null
}
Sometimes it is easier to request this with Rx methodology. Here's the code:
private fun getArAvailabilityRx(context: Context): Single<ArCoreApk.Availability> {
return Single.fromCallable<ArCoreApk.Availability> {
ArCoreApk.getInstance().checkAvailability(context)
}.flatMap { availability ->
if (availability.isTransient) {
// `isTransient` means it hasn't finished loading value; let's request the value in 500 ms
getArAvailabilityRx(context).delaySubscription(500, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
} else {
Single.just(availability)
}
}.observeOn(AndroidSchedulers.mainThread())
}
Here's a little utility class I wrote (based originally on something from https://github.com/google/helloargdx).
It will perform all the checks and setup necessary, in order to ensure it is safe to launch a Session.
abstract class ArCheckFragment : Fragment() {
private var userRequestedInstall = true
abstract fun onCameraPermissionDeny()
abstract fun onArCoreUnavailable(availability: Availability)
abstract fun onArCoreInstallFail(exception: UnavailableException)
abstract fun onArCoreInstallSuccess()
override fun onResume() {
super.onResume()
performCheck()
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == REQUEST_CODE_CAMERA_PERMISSION) {
for (i in permissions.indices) {
if (permissions[i] == Manifest.permission.CAMERA &&
grantResults[i] == PackageManager.PERMISSION_GRANTED
) {
checkArCore()
return
}
}
onCameraPermissionDeny()
}
}
/**
* Performs the whole check
*/
fun performCheck() {
if (requestCameraPermission()) {
checkArCore()
}
}
/**
* Requests the camera permission, if necessary.
* #return whether camera permission is already granted. If so, the permission won't be requested.
*/
private fun requestCameraPermission(): Boolean {
if (ContextCompat.checkSelfPermission(
requireContext(),
Manifest.permission.CAMERA
) == PackageManager.PERMISSION_GRANTED
) {
return true
}
requestPermissions(arrayOf(Manifest.permission.CAMERA), REQUEST_CODE_CAMERA_PERMISSION)
return false
}
private fun checkArCore() {
if (!isResumed) {
return
}
val availability = ArCoreApk.getInstance().checkAvailability(activity)
if (availability.isTransient) {
requireView().postDelayed(AR_CORE_CHECK_INTERVAL) { checkArCore() }
return
}
when (availability) {
Availability.SUPPORTED_INSTALLED ->
onArCoreInstallSuccess()
Availability.SUPPORTED_APK_TOO_OLD,
Availability.SUPPORTED_NOT_INSTALLED ->
startArCoreInstallation()
else ->
onArCoreUnavailable(availability)
}
}
private fun startArCoreInstallation() {
try {
val installStatus =
ArCoreApk.getInstance().requestInstall(activity, userRequestedInstall)
when (installStatus) {
InstallStatus.INSTALLED -> onArCoreInstallSuccess()
InstallStatus.INSTALL_REQUESTED,
null ->
// Ensures next invocation of requestInstall() will either return
// INSTALLED or throw an exception.
userRequestedInstall = false
}
} catch (exception: UnavailableException) {
onArCoreInstallFail(exception)
}
}
companion object {
private const val REQUEST_CODE_CAMERA_PERMISSION = 1
private const val AR_CORE_CHECK_INTERVAL = 200L
}
}
You can subclass this Fragment and implement the abstract functions to receive callbacks on what the result of these checks is. Only in onArCoreInstallSuccess is it safe to create a Session.
I am trying to authenticate client token created by Firebase authentication library in Android in GCE endpoint.
The guide of how to do this can be found here
Basically I need to call this code snippet from the end point (i.e. server backend code not android code).
FirebaseAuth.getInstance().verifyIdToken(idToken)
.addOnSuccessListener(new OnSuccessListener<FirebaseToken>() {
#Override
public void onSuccess(FirebaseToken decodedToken) {
String uid = decodedToken.getUid();
// ...
}
});
Let say I want to execute that code and return the user to android client code. How should I do that?
This is my sample code that does not make sense. But it demonstrate what I want to do!
#ApiMethod(name = "serverAuth")
public MyUser serverAuth(#Named("token") String token) {
FirebaseAuth.getInstance().verifyIdToken(token)
.addOnSuccessListener(new OnSuccessListener<FirebaseToken>() {
#Override
public void onSuccess(FirebaseToken decodedToken) {
String uid = decodedToken.getUid();
String email = decodedToken.getEmail();
String name = decodedToken.getName();
Map<String, Object> claims = decodedToken.getClaims();
String claimString = "";
for (Object claim : claims.values()) {
claimString += claims.toString();
}
MyUser user = new MyUser(uid, email, name, claimString);
//How to return this user?
}
});
//This is compile error since user varriable does not exist here
return user;
}
I have google search how to execute async code in GCE endpoints. But getting nowhere with that. What I get is something about code execution that is blocking until done and then return the user. But how to code so that async code as above become blocking?
CountDownLatch is the magic class you need. It will let you wait till the OnSuccessListener is actually completed.
Adapt your method this way: (I removed the steps that lead to MyUser's creation in order to focus on important points.)
#ApiMethod(name = "serverAuth")
public MyUser serverAuth(#Named("token") String token) {
final List<MyUser> users = new ArrayList<>();
final CountDownLatch cdl = new CountDownLatch(1);
FirebaseAuth.getInstance().verifyIdToken(token)
.addOnSuccessListener(new OnSuccessListener<FirebaseToken>() {
#Override
public void onSuccess(FirebaseToken decodedToken) {
// ... init uid, email, name and claimString
users.add(new MyUser(uid, email, name, claimString));
cdl.countDown();
}
});
try {
cdl.await(); // This line blocks execution till count down latch is 0
} catch (InterruptedException ie) {
}
if (users.size() > 0) {
return users.get(0);
} else {
return null ;
}
}
This is the basic version of what you need. IMHO, it requires 2 more improvements :
You should also take the possibility of failure into account :
FirebaseAuth.getInstance().verifyIdToken(token)
.addOnSuccessListener(new OnSuccessListener<FirebaseToken>() {
#Override
public void onSuccess(FirebaseToken decodedToken) {
cdl.countDown();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
// log error, ...
cdl.countDown();
}
});
You should also take the possibility that none of the listeners are called. In this situation your method will never return. To avoid that, you can set a timeout on the await() method :
try {
// This line blocks execution till count down latch is 0
// or after 30 seconds.
cdl.await(30l, TimeUnit.SECONDS);
} catch (InterruptedException ie) {
}
That's it. Hope this may help.
I am messing around some with the google awareness api and now my understanding of RxJava is limiting me.
What I want to achieve in the end:
I want to get a Weather and a Location from the Api, and merge them into one object that I can pass on to my view for update.
However, I'm not sure how I achieve the returning of an Observable from the api callback here since it has void return type, and how to achieve merging of the weather and location object from api.getWeather and api.getLocation
public void requestUserCurrentInfo() {
Subscription userInfo = getWeatherLocation().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()).subscribe(userinfo ->
Log.d(TAG,userinfo.something()));
}
public Observable<UserInfo> getWeatherLocation () {
try {
Awareness.SnapshotApi.getWeather(client)
.setResultCallback(weather -> {
if (!weather.getStatus().isSuccess()) {
Log.d(TAG, "Could not get weather");
return;
}
//How do I do here?
return weather.getWeather();
});
Awareness.SnapshotApi.getLocation(mGoogleApiClient)
.setResultCallback(retrievedLocation -> {
if(!retrievedLocation.getStatus().isSuccess()) return;
Log.d("FRAG", retrievedLocation.getLocation().getLatitude() + "");
});
} catch (SecurityException exception) {
throw new SecurityException("No permission " + exception);
}
}
For my other things in my Project, I get some stuff through a REST api following the repository pattern, then I can get it like this because every step returns a Observable< SmhiResponse >
getWeatherSubscription = getWeatherUsecase.execute().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()).subscribe(
smhiResponseModel -> {Log.d(TAG,"Retrieved weather"); locationView.hideLoading();},
err -> {Log.d(TAG,"Error fetching weather"); locationView.hideLoading();}
);
You don't return an observable from the callback but wrap your callbacks into observables to make them combinable (untested):
Observable<WeatherResult> weatherObservable = Observable.create(subscriber -> {
Awareness.SnapshotApi.getWeather(client)
.setResultCallback(weather -> {
if (!weather.getStatus().isSuccess()) {
subscriber.onError(new Exception("Could not get weather."));
Log.d(TAG, "Could not get weather");
} else {
//How do I do here?
subscriber.onNext(weather);
subscriber.onCompleted();
}
});
});
Observable<LocationResult> locationObservable = Observable.create(subscriber -> {
Awareness.SnapshotApi.getLocation(mGoogleApiClient)
.setResultCallback(retrievedLocation -> {
if(!retrievedLocation.getStatus().isSuccess()) {
subscriber.onError(new Exception("Could not get location."));
} else {
Log.d("FRAG", retrievedLocation.getLocation().getLatitude() + "");
subscriber.onNext(retrievedLocation);
subscriber.onCompleted();
}
});
});
now combine them via .combineLatest() or .zip():
Observable<CombinedResult> combinedResults = Observable.zip(weatherObservable, locationObservable,
(weather, location) -> {
/* somehow combine weather and location then return as type "CombinedResult" */
});
don't forget to subscribe, otherwise none of them gets executed:
combinedResults.subscribe(combinedResult -> {/*do something with that stuff...*/});
Observable.combineLatest(getWeather (), getLocation(), new Func2<List<Object_A>, List<Object_B>, Object>() {
#Override
public Object call(Object o, Object o2) {
combine both results and return the combine result to observer
}
})
getweather() and getlocation() return observables