Skip to content
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 @@ -21,6 +21,8 @@
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;

import com.mongodb.stitch.core.internal.net.NetworkMonitor;

import java.util.HashSet;
Expand Down Expand Up @@ -56,9 +58,39 @@ public synchronized void removeNetworkStateListener(@Nonnull final StateListener

@Override
public void onReceive(final Context context, final Intent intent) {
final Set<StateListener> listenersCopy = new HashSet<>(listeners);
for (final StateListener listener : listenersCopy) {
listener.onNetworkStateChanged();
// Dispatch our network change callback to a background thread,
// since our listeners may block the main thread.
// See https://developer.android.com/guide/components/broadcasts#effects-process-state
final PendingResult pendingResult = goAsync();
final NetworkStateChangedTask asyncTask =
new NetworkStateChangedTask(pendingResult, new HashSet<>(listeners));
asyncTask.execute();
}

private static final class NetworkStateChangedTask extends AsyncTask<Void, Void, Void> {
private final Set<StateListener> listeners;
private final PendingResult pendingResult;

private NetworkStateChangedTask(
final PendingResult result,
final Set<StateListener> listeners
) {
this.pendingResult = result;
this.listeners = listeners;
}

@Override
protected Void doInBackground(final Void... v) {
for (final StateListener listener: listeners) {
listener.onNetworkStateChanged();
}
return null;
}

@Override
protected void onPostExecute(final Void v) {
super.onPostExecute(v);
pendingResult.finish();
}
}
}
1 change: 1 addition & 0 deletions android/examples/stress-tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
28 changes: 28 additions & 0 deletions android/examples/stress-tests/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
apply plugin: 'com.android.application'
apply plugin: 'digital.wup.android-maven-publish'
apply plugin: 'jacoco-android'

buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'digital.wup:android-maven-publish:3.3.0'
classpath 'com.dicedmelon.gradle:jacoco-android:0.1.4'
}
}

android {
compileSdkVersion target_api
defaultConfig {
minSdkVersion min_api
targetSdkVersion target_api
}
}

dependencies {
implementation project(':android:stitch-android-sdk')
implementation project(':android:android-services:stitch-android-services-mongodb-remote')
implementation "com.android.support:support-v4:${support_library_version}"
implementation "com.android.support:appcompat-v7:${support_library_version}"
implementation "com.android.support:recyclerview-v7:${support_library_version}"
implementation "com.android.support.constraint:constraint-layout:1.1.0"
}
21 changes: 21 additions & 0 deletions android/examples/stress-tests/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
21 changes: 21 additions & 0 deletions android/examples/stress-tests/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mongodb.stitch.android.examples.stresstests">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
* Copyright 2018-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.mongodb.stitch.android.examples.stresstests;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.mongodb.stitch.android.core.Stitch;
import com.mongodb.stitch.android.core.StitchAppClient;
import com.mongodb.stitch.android.core.auth.StitchUser;
import com.mongodb.stitch.android.services.mongodb.remote.RemoteMongoClient;
import com.mongodb.stitch.android.services.mongodb.remote.RemoteMongoCollection;
import com.mongodb.stitch.android.services.mongodb.remote.Sync;
import com.mongodb.stitch.core.auth.providers.anonymous.AnonymousCredential;
import com.mongodb.stitch.core.services.mongodb.remote.sync.DefaultSyncConflictResolvers;

import java.util.ArrayList;
import java.util.Locale;
import java.util.Random;

import org.bson.Document;
import org.bson.types.ObjectId;

public class MainActivity extends AppCompatActivity {

private Button addSingleDocButton;
private Button addManyDocsButton;
private Button clearDocsButton;

private EditText etManyDocs;
private TextView label;

private StitchAppClient stitchAppClient;
private RemoteMongoCollection<Document> coll;
private Sync<Document> syncedColl;

private static final String TAG = MainActivity.class.getName();

private static final Random RANDOM = new Random();

private void initializeSync() {
stitchAppClient.getAuth().loginWithCredential(new AnonymousCredential()).addOnSuccessListener(
stitchUser -> {
coll = stitchAppClient
.getServiceClient(RemoteMongoClient.factory, "mongodb-atlas")
.getDatabase("stress")
.getCollection("tests");

syncedColl = coll.sync();
syncedColl.configure(
DefaultSyncConflictResolvers.remoteWins(),
(documentId, event) -> {
Log.i(TAG, String.format("Got event for doc %s: %s", documentId, event));
updateLabels();
},
(documentId, error) -> Log.e(
TAG, String.format("Got sync error for doc %s: %s", documentId, error)
)
);
updateLabels();
});

}

private void updateLabels() {
syncedColl.getSyncedIds().addOnSuccessListener(syncedIds -> label.setText(String.format(
Locale.US,
"# of synced docs: %d",
syncedIds.size()
)));
}

private void syncNewDocument() {
final StitchUser user = stitchAppClient.getAuth().getUser();
if (user == null) {
return;
}
final Document docToInsert = new Document()
.append("_id", new ObjectId())
.append("owner_id", stitchAppClient.getAuth().getUser().getId())
.append("message", String.format(Locale.US, "%d", RANDOM.nextInt()));

syncedColl.insertOne(docToInsert).addOnCompleteListener(task -> updateLabels());
}

private void syncManyDocuments(final int numberOfDocs) {
final StitchUser user = stitchAppClient.getAuth().getUser();
if (user == null) {
return;
}

final ArrayList<Document> docsToInsert = new ArrayList<>();

for (int i = 0; i < numberOfDocs; ++i) {
final Document docToInsert = new Document()
.append("_id", new ObjectId())
.append("owner_id", stitchAppClient.getAuth().getUser().getId())
.append("message", String.format(Locale.US, "%d", RANDOM.nextInt()));

docsToInsert.add(docToInsert);
}

syncedColl.insertMany(docsToInsert).addOnCompleteListener(task -> updateLabels());
}

private void clearAllDocuments() {
final StitchUser user = stitchAppClient.getAuth().getUser();
if (user == null) {
return;
}

syncedColl.deleteMany(new Document()).addOnCompleteListener(task -> updateLabels());
}

@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (!Stitch.hasAppClient("stitch-tests-js-sdk-jntlj")) {
Stitch.initializeDefaultAppClient("stitch-tests-js-sdk-jntlj");
}

stitchAppClient = Stitch.getDefaultAppClient();
initializeSync();

addSingleDocButton = findViewById(R.id.button);
addManyDocsButton = findViewById(R.id.button3);
clearDocsButton = findViewById(R.id.button2);
etManyDocs = findViewById(R.id.numInput);

label = findViewById(R.id.textView);

addSingleDocButton.setOnClickListener(v -> syncNewDocument());

addManyDocsButton.setOnClickListener(v -> {
final int numberOfDocsToInsert = Integer.decode(etManyDocs.getText().toString());
syncManyDocuments(numberOfDocsToInsert);
});

clearDocsButton.setOnClickListener(v -> clearAllDocuments());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path
android:fillType="evenOdd"
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
android:strokeWidth="1"
android:strokeColor="#00000000">
<aapt:attr name="android:fillColor">
<gradient
android:endX="78.5885"
android:endY="90.9159"
android:startX="48.7653"
android:startY="61.0927"
android:type="linear">
<item
android:color="#44000000"
android:offset="0.0" />
<item
android:color="#00000000"
android:offset="1.0" />
</gradient>
</aapt:attr>
</path>
<path
android:fillColor="#FFFFFF"
android:fillType="nonZero"
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
android:strokeWidth="1"
android:strokeColor="#00000000" />
</vector>
Loading