Skip to content

Commit

Permalink
Remove install referrer receiver - only google library now.
Browse files Browse the repository at this point in the history
Add more info to craslytics reports.
  • Loading branch information
kenumir committed Nov 15, 2019
1 parent 4eb5dc8 commit 827aad3
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 73 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "com.wt.pinger"
minSdkVersion versions.minSdkVersion
targetSdkVersion versions.targetSdkVersion
versionCode 95
versionName "3.4.7"
versionCode 96
versionName "3.4.8"

resConfigs "en", "pl"
vectorDrawables.useSupportLibrary = true
Expand Down
10 changes: 0 additions & 10 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,6 @@
android:value=".activity.MainActivity" />
</activity>

<!-- receiver -->
<receiver
android:name=".receivers.AppInstallReferrerReceiver"
android:exported="true"
tools:ignore="ExportedReceiver">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER"/>
</intent-filter>
</receiver>

<!-- service -->
<service
android:name=".service.CmdService"
Expand Down
11 changes: 9 additions & 2 deletions app/src/main/java/com/wt/pinger/App.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.wt.pinger;

import android.app.Application;
import android.os.Build;
import android.os.SystemClock;

import androidx.appcompat.app.AppCompatDelegate;
Expand All @@ -18,6 +19,7 @@
import com.wt.pinger.proto.ping.PingManager;
import com.wt.pinger.utils.PingProgram;
import com.wt.pinger.utils.Prefs;
import com.wt.pinger.utils.VariousUtils;

import java.io.BufferedReader;
import java.io.File;
Expand Down Expand Up @@ -121,9 +123,14 @@ protected Void doInBackground(Void... voids) {
ERA.log("App.AsyncTask:UUID load");
}

if (!BuildConfig.DEBUG) {
Crashlytics.setUserIdentifier(uuid);
Crashlytics.setUserIdentifier(uuid);
Crashlytics.setString("Installer Package", VariousUtils.getAppInstallerPackage(getApplicationContext()));
Crashlytics.setString("App Fingerprint", VariousUtils.getAppFingerprint(getApplicationContext()));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Crashlytics.setString("App Standby Bucket Name", VariousUtils.getAppStandbyBucketName(getApplicationContext()));
}
Crashlytics.setBool("Test Lab Device", VariousUtils.isTestLabDevice(getApplicationContext()));
Crashlytics.setBool("Background Restricted", VariousUtils.isBackgroundRestricted(getApplicationContext()));

File pingFile = new File("/system/bin/ping");
if (pingFile.exists()) {
Expand Down
39 changes: 25 additions & 14 deletions app/src/main/java/com/wt/pinger/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.view.Menu;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
Expand Down Expand Up @@ -110,15 +111,22 @@ public boolean onTabSelected(int position, boolean wasSelected) {
.replace(R.id.mainFrame, mainFragments[0])
.commit();
UserSync.get().saveUser(this);
}

mReferrerClient = InstallReferrerClient.newBuilder(this).build();
try {
mReferrerClient.startConnection(this);
} catch (Exception e) {
ERA.logException(e);
Prefs.getAsync(this, prefs -> {
if (!prefs.load(Constants.PREF_REFERRER_SAVED, false)) {
mReferrerClient = InstallReferrerClient.newBuilder(MainActivity.this).build();
try {
mReferrerClient.startConnection(MainActivity.this);
} catch (Exception e) {
ERA.logException(e);
}
}
});

}



ERA.log("MainActivity.onCreate:end");
}

Expand Down Expand Up @@ -149,7 +157,7 @@ protected void onResume() {
}

@Override
protected void onSaveInstanceState(Bundle outState) {
protected void onSaveInstanceState(@NonNull Bundle outState) {
saveInstanceStateCalled = true;
super.onSaveInstanceState(outState);
}
Expand All @@ -173,13 +181,10 @@ public void onInstallReferrerSetupFinished(int responseCode) {
Console.logi("onInstallReferrerSetupFinished: InstallReferrer=" + response.getInstallReferrer());
}
ERA.log("onInstallReferrerSetupFinished: InstallReferrer=" + response.getInstallReferrer());
Prefs.getAsync(ctx, new Prefs.OnPrefsReady() {
@Override
public void onReady(Prefs prefs) {
if (!prefs.load(Constants.PREF_REFERRER_SAVED, false)) {
prefs.save(Constants.PREF_REFERRER, response.getInstallReferrer());
UserSync.get().saveUser(ctx);
}
Prefs.getAsync(ctx, prefs -> {
if (!prefs.load(Constants.PREF_REFERRER_SAVED, false)) {
prefs.save(Constants.PREF_REFERRER, response.getInstallReferrer());
UserSync.get().saveUser(ctx);
}
});
mReferrerClient.endConnection();
Expand All @@ -195,6 +200,12 @@ public void onReady(Prefs prefs) {
break;
default:
ERA.log("onInstallReferrerSetupFinished: responseCode=" + responseCode + ", response not found");
case InstallReferrerClient.InstallReferrerResponse.DEVELOPER_ERROR:
ERA.log("onInstallReferrerSetupFinished: responseCode=" + responseCode + ", DEVELOPER_ERROR");
break;
case InstallReferrerClient.InstallReferrerResponse.SERVICE_DISCONNECTED:
ERA.log("onInstallReferrerSetupFinished: responseCode=" + responseCode + ", SERVICE_DISCONNECTED");
break;
}
}

Expand Down

This file was deleted.

131 changes: 131 additions & 0 deletions app/src/main/java/com/wt/pinger/utils/VariousUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.wt.pinger.utils;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.usage.UsageStatsManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.hivedi.era.ERA;

import java.security.MessageDigest;
import java.util.Locale;

public class VariousUtils {

@NonNull
@SuppressLint("PackageManagerGetSignatures")
public static String getAppFingerprint(@NonNull Context ctx) {
final PackageManager pm = ctx.getPackageManager();
try {
/*
* Może niestety zdazyć się błąd typu:
* RuntimeException Package manager has died
* Caused By: android.os.DeadObjectException
* Pojawia sie on gdy PackageManager z jakiegos powodu zawiesił się,
* mogła spowodować to inna aplikacja, w naszym przypadku zapytanie ograniczone
* jest do minimum, pytamy tylko o nasz package + sygantury więc mniej już nie można
*/
PackageInfo info = pm.getPackageInfo(ctx.getPackageName(), PackageManager.GET_SIGNATURES);
String[] appCert = new String[info.signatures.length];
int i = 0;
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
StringBuilder s = new StringBuilder();
for (byte b : md.digest()) {
s.append(":").append(String.format("%02x", b));
}
appCert[i] = s.substring(1).toUpperCase(Locale.getDefault());
i++;
}
return flatArray(appCert, ", ");
} catch (Throwable e) {
ERA.logException(e);
return "Error " + e;
}
}

@NonNull
public static String getAppInstallerPackage(@NonNull Context ctx) {
try {
String res = ctx.getPackageManager().getInstallerPackageName(ctx.getPackageName());
return TextUtils.isEmpty(res) ? "Empty" : res;
} catch (Throwable e) {
return "Error " + e;
}
}

public static boolean isTestLabDevice(@NonNull Context ctx) {
try {
// INFO sprawdzenie czy to urządzenie to test lab
// https://stackoverflow.com/a/43598581/959086
// https://firebase.google.com/docs/test-lab/android/android-studio#modify_instrumented_test_behavior_for_testlab
String testLabSetting = Settings.System.getString(ctx.getContentResolver(), "firebase.test.lab");
return "true".equals(testLabSetting);
} catch (Exception e) {
return false;
}
}

public static boolean isBackgroundRestricted(@NonNull Context ctx) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ActivityManager am = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
if (am != null) {
return am.isBackgroundRestricted();
}
}
return false;
}

@RequiresApi(api = Build.VERSION_CODES.P)
@NonNull
public static String getAppStandbyBucketName(@NonNull Context ctx) {
try {
UsageStatsManager sm = (UsageStatsManager) ctx.getSystemService(Context.USAGE_STATS_SERVICE);
if (sm != null) {
int bucketInt = sm.getAppStandbyBucket();
String bucket = "Unknown (" + bucketInt + ")";
switch (bucketInt) {
case UsageStatsManager.STANDBY_BUCKET_ACTIVE:
bucket = "BUCKET_ACTIVE";
break;
case UsageStatsManager.STANDBY_BUCKET_RARE:
bucket = "BUCKET_RARE";
break;
case UsageStatsManager.STANDBY_BUCKET_WORKING_SET:
bucket = "BUCKET_WORKING_SET";
break;
case UsageStatsManager.STANDBY_BUCKET_FREQUENT:
bucket = "BUCKET_FREQUENT";
break;
}
return bucket;
} else {
return "UsageStatsManager is NULL";
}
} catch (Exception e) {
return "Error " + e;
}
}

public static String flatArray(String[] s, String separator) {
StringBuilder res = new StringBuilder();
if (s != null && s.length > 0) {
for(String i : s) {
res.append(i).append(separator == null ? "" : separator);
}
res = new StringBuilder(res.substring(0, res.length() - (separator == null ? 0 : separator.length())));
}
return res.toString();
}

}

0 comments on commit 827aad3

Please sign in to comment.