Skip to content

refactor(analytics, firestore, mlkit): fix deprecation warnings #424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public void onAdRevenuePaid(MaxAd impressionData) {
Bundle params = new Bundle();
params.putString(FirebaseAnalytics.Param.AD_PLATFORM, "appLovin");
params.putString(FirebaseAnalytics.Param.AD_SOURCE, impressionData.getNetworkName());
params.putString(FirebaseAnalytics.Param.AD_FORMAT, impressionData.getFormat().getDisplayName());
params.putString(FirebaseAnalytics.Param.AD_FORMAT, impressionData.getFormat().getLabel());
params.putString(FirebaseAnalytics.Param.AD_UNIT_NAME, impressionData.getAdUnitId());
params.putDouble(FirebaseAnalytics.Param.VALUE, revenue);
params.putString(FirebaseAnalytics.Param.CURRENCY, "USD"); // All Applovin revenue is sent in USD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class MainActivity :
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.AD_IMPRESSION) {
param(FirebaseAnalytics.Param.AD_PLATFORM, "appLovin")
param(FirebaseAnalytics.Param.AD_UNIT_NAME, impressionData.adUnitId)
param(FirebaseAnalytics.Param.AD_FORMAT, impressionData.format.displayName)
param(FirebaseAnalytics.Param.AD_FORMAT, impressionData.format.label)
param(FirebaseAnalytics.Param.AD_SOURCE, impressionData.networkName)
param(FirebaseAnalytics.Param.VALUE, impressionData.revenue)
param(FirebaseAnalytics.Param.CURRENCY, "USD") // All Applovin revenue is sent in USD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,6 @@ void runAll() {
}
}


void deleteAll() {
deleteCollection("cities");
deleteCollection("users");
}

private void deleteCollection(final String path) {
deleteCollection(db.collection(path), 50, EXECUTOR);
}

public void setup() {
// [START get_firestore_instance]
FirebaseFirestore db = FirebaseFirestore.getInstance();
Expand Down Expand Up @@ -1212,63 +1202,6 @@ public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
// [END fs_collection_group_query]
}

// [START delete_collection]
/**
* Delete all documents in a collection. Uses an Executor to perform work on a background
* thread. This does *not* automatically discover and delete subcollections.
*/
private Task<Void> deleteCollection(final CollectionReference collection,
final int batchSize,
Executor executor) {

// Perform the delete operation on the provided Executor, which allows us to use
// simpler synchronous logic without blocking the main thread.
return Tasks.call(executor, new Callable<Void>() {
@Override
public Void call() throws Exception {
// Get the first batch of documents in the collection
Query query = collection.orderBy(FieldPath.documentId()).limit(batchSize);

// Get a list of deleted documents
List<DocumentSnapshot> deleted = deleteQueryBatch(query);

// While the deleted documents in the last batch indicate that there
// may still be more documents in the collection, page down to the
// next batch and delete again
while (deleted.size() >= batchSize) {
// Move the query cursor to start after the last doc in the batch
DocumentSnapshot last = deleted.get(deleted.size() - 1);
query = collection.orderBy(FieldPath.documentId())
.startAfter(last.getId())
.limit(batchSize);

deleted = deleteQueryBatch(query);
}

return null;
}
});

}

/**
* Delete all results from a query in a single WriteBatch. Must be run on a worker thread
* to avoid blocking/crashing the main thread.
*/
@WorkerThread
private List<DocumentSnapshot> deleteQueryBatch(final Query query) throws Exception {
QuerySnapshot querySnapshot = Tasks.await(query.get());

WriteBatch batch = query.getFirestore().batch();
for (QueryDocumentSnapshot snapshot : querySnapshot) {
batch.delete(snapshot.getReference());
}
Tasks.await(batch.commit());

return querySnapshot.getDocuments();
}
// [END delete_collection]

public void toggleOffline() {
// [START disable_network]
db.disableNetwork()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
Expand Down Expand Up @@ -85,24 +86,8 @@ public void onFailure(@NonNull Exception e) {
}

private void onDeleteAllClicked() {
FirebaseAuth.getInstance()
.signInAnonymously()
.addOnSuccessListener(this, new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
Log.d(TAG, "auth:onSuccess");

// Delete
DocSnippets docSnippets = new DocSnippets(mFirestore);
docSnippets.deleteAll();
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "auth:onFailure", e);
}
});
// See: https://firebase.google.com/docs/firestore/manage-data/delete-data#collections
Toast.makeText(this, "Deleting all is no longer supported", Toast.LENGTH_LONG).show();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1001,62 +1001,6 @@ abstract class DocSnippets(val db: FirebaseFirestore) {
// [END fs_collection_group_query]
}

// [START delete_collection]
/**
* Delete all documents in a collection. Uses an Executor to perform work on a background
* thread. This does *not* automatically discover and delete subcollections.
*/
private fun deleteCollection(
collection: CollectionReference,
batchSize: Int,
executor: Executor
): Task<Void> {

// Perform the delete operation on the provided Executor, which allows us to use
// simpler synchronous logic without blocking the main thread.
return Tasks.call(executor, Callable<Void> {
// Get the first batch of documents in the collection
var query = collection.orderBy(FieldPath.documentId()).limit(batchSize.toLong())

// Get a list of deleted documents
var deleted = deleteQueryBatch(query)

// While the deleted documents in the last batch indicate that there
// may still be more documents in the collection, page down to the
// next batch and delete again
while (deleted.size >= batchSize) {
// Move the query cursor to start after the last doc in the batch
val last = deleted[deleted.size - 1]
query = collection.orderBy(FieldPath.documentId())
.startAfter(last.id)
.limit(batchSize.toLong())

deleted = deleteQueryBatch(query)
}

null
})
}

/**
* Delete all results from a query in a single WriteBatch. Must be run on a worker thread
* to avoid blocking/crashing the main thread.
*/
@WorkerThread
@Throws(Exception::class)
private fun deleteQueryBatch(query: Query): List<DocumentSnapshot> {
val querySnapshot = Tasks.await(query.get())

val batch = query.firestore.batch()
for (snapshot in querySnapshot) {
batch.delete(snapshot.reference)
}
Tasks.await(batch.commit())

return querySnapshot.documents
}
// [END delete_collection]

fun toggleOffline() {
// [START disable_network]
db.disableNetwork().addOnCompleteListener {
Expand Down
1 change: 0 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ include ':auth:app',
':admob:app',
':messaging:app',
':crashlytics:app',
':mlkit:app',
':perf:app',
':test-lab:app',
':analytics:app',
Expand Down