Skip to content

Commit

Permalink
Update to newer Crashlytics, which doesn't work on API < 8.
Browse files Browse the repository at this point in the history
- Add Util.supportCrashlytics to determine if if should be enabled.

- Add Util.crashlytics* to forward to the Crashlytics methods only if
  it's enabled.

- Disable Crashlytics for debug builds.

- Update HomeActivity to conditionally enable it.
  • Loading branch information
nikclayton committed May 23, 2016
1 parent bb1b8d4 commit 484ea34
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 28 deletions.
16 changes: 11 additions & 5 deletions Squeezer/build.gradle
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
buildscript {
repositories {
mavenCentral()
maven { url 'http://download.crashlytics.com/maven' }
jcenter()
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
classpath 'com.github.triplet.gradle:play-publisher:1.1.0'
classpath 'io.fabric.tools:gradle:1.+'
}
}

apply plugin: 'com.android.application'
apply plugin: 'crashlytics'
apply plugin: 'io.fabric'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'com.github.triplet.play'
apply plugin: 'uk.org.ngo.gradle.whatsnew'

repositories {
mavenCentral()
maven { url 'http://download.crashlytics.com/maven' }
maven { url 'https://maven.fabric.io/public' }
}

dependencies {
Expand Down Expand Up @@ -49,7 +49,9 @@ dependencies {
compile 'com.bluelinelabs:logansquare:1.0.6'

// Crashlytics.
compile 'com.crashlytics.android:crashlytics:1.+'
compile('com.crashlytics.sdk.android:crashlytics:2.5.5@aar') {
transitive = true
}

// KitKat time picker
compile 'com.nineoldandroids:library:2.4.0'
Expand Down Expand Up @@ -106,6 +108,10 @@ android {
}

buildTypes {
debug {
ext.enableCrashlytics = false
}

release {
minifyEnabled true
signingConfig signingConfigs.release
Expand Down
6 changes: 6 additions & 0 deletions Squeezer/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="uk.org.ngo.squeezer"
android:versionCode="13" android:versionName="0.9.1">

<!-- Crashlytics is for API 8+. Override that here, as Squeezer checks for
the API level before invoking it. -->
<uses-sdk android:targetSdkVersion="21" android:minSdkVersion="7"
tools:overrideLibrary="com.crashlytics.android,com.crashlytics.android.answers,io.fabric.sdk.android,com.crashlytics.android.core,com.crashlytics.android.beta"/>

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
Expand Down
8 changes: 5 additions & 3 deletions Squeezer/src/main/java/uk/org/ngo/squeezer/HomeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.crashlytics.android.Crashlytics;
import com.google.android.apps.analytics.GoogleAnalyticsTracker;

import com.crashlytics.android.Crashlytics;

import java.util.ArrayList;
import java.util.List;

import de.cketti.library.changelog.ChangeLog;
import io.fabric.sdk.android.Fabric;
import uk.org.ngo.squeezer.dialog.TipsDialog;
import uk.org.ngo.squeezer.framework.BaseActivity;
import uk.org.ngo.squeezer.itemlist.AlbumListActivity;
Expand Down Expand Up @@ -97,8 +98,9 @@ public class HomeActivity extends BaseActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!BuildConfig.DEBUG) {
Crashlytics.start(this);

if (Util.supportCrashlytics()) {
Fabric.with(this, new Crashlytics());
}

setContentView(R.layout.item_list);
Expand Down
69 changes: 69 additions & 0 deletions Squeezer/src/main/java/uk/org/ngo/squeezer/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
package uk.org.ngo.squeezer;

import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.TextView;

import com.crashlytics.android.Crashlytics;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
Expand Down Expand Up @@ -220,4 +223,70 @@ public static View setAlpha(View view, float alpha) {

return view;
}

/** @return True if Crashlytics is supported in this build. */
public static boolean supportCrashlytics() {
// Don't include in debug builds
if (BuildConfig.DEBUG) {
return false;
}

// Don't include in <= API 7 (only works on API 8 and above).
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ECLAIR_MR1) {
return false;
}

return true;
}

/**
* Calls {@link Crashlytics#setString(String, String)} if Crashlytics is
* enabled in this build, otherwise does nothing.
*
* @param key
* @param value
*/
public static void crashlyticsSetString(String key, String value) {
if (supportCrashlytics()) {
Crashlytics.setString(key, value);
}
}

/**
* Calls {@link Crashlytics#log(String)} if Crashlytics is enabled in
* this build, otherwise does nothing.
*
* @param msg
*/
public static void crashlyticsLog(String msg) {
if (supportCrashlytics()) {
Crashlytics.log(msg);
}
}

/**
* Calls {@link Crashlytics#log(int, String, String)} if Crashlytics is
* enabled in this build, otherwise does nothing.
*
* @param priority
* @param tag
* @param msg
*/
public static void crashlyticsLog(int priority, String tag, String msg) {
if (supportCrashlytics()) {
Crashlytics.log(priority, tag, msg);
}
}

/**
* Calls {@link Crashlytics#logException(Throwable)} if Crashlytics is
* enabled in this build, otherwise does nothing.
*
* @param throwable
*/
public static void crashlyticsLogException(java.lang.Throwable throwable) {
if (supportCrashlytics()) {
Crashlytics.logException(throwable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import android.support.annotation.Nullable;
import android.util.Log;

import com.crashlytics.android.Crashlytics;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
Expand Down Expand Up @@ -406,9 +405,9 @@ synchronized void sendCommandImmediately(String... commands) {

// Make sure that username/password do not make it to Crashlytics.
if (commands[0].startsWith("login ")) {
Crashlytics.setString("lastCommands", "login [username] [password]");
Util.crashlyticsSetString("lastCommands", "login [username] [password]");
} else {
Crashlytics.setString("lastCommands", formattedCommands);
Util.crashlyticsSetString("lastCommands", formattedCommands);
}

writer.println(formattedCommands);
Expand Down Expand Up @@ -1022,7 +1021,7 @@ public void handle(List<String> tokens) {
mUrlPrefix = "http://" + getCurrentHost() + ":" + getHttpPort();
String version = tokens.get(1);
connectionState.setServerVersion(version);
Crashlytics.setString("server_version", version);
Util.crashlyticsSetString("server_version", version);

mEventBus.postSticky(new HandshakeComplete(
connectionState.canFavorites(), connectionState.canMusicfolder(),
Expand Down Expand Up @@ -1300,9 +1299,9 @@ void onLineReceived(String serverLine) {

// Make sure that username/password do not make it to Crashlytics.
if (serverLine.startsWith("login ")) {
Crashlytics.setString("lastReceivedLine", "login [username] [password]");
Util.crashlyticsSetString("lastReceivedLine", "login [username] [password]");
} else {
Crashlytics.setString("lastReceivedLine", serverLine);
Util.crashlyticsSetString("lastReceivedLine", serverLine);
}

List<String> tokens = Arrays.asList(mSpaceSplitPattern.split(serverLine));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
import android.os.Environment;
import android.util.Log;

import com.crashlytics.android.Crashlytics;

import java.io.File;

import uk.org.ngo.squeezer.Util;


/**
* Handle events from the download manager
Expand Down Expand Up @@ -86,15 +86,15 @@ private void handleDownloadComplete(Context context, long id) {
if (tempFile.renameTo(localFile)) {
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(localFile)));
} else {
Crashlytics.log(Log.ERROR, TAG, "Could not rename [" + tempFile + "] to [" + localFile + "]");
Util.crashlyticsLog(Log.ERROR, TAG, "Could not rename [" + tempFile + "] to [" + localFile + "]");
}
break;
default:
Crashlytics.log(Log.ERROR, TAG, "Unsuccessful download " + format(status, reason, title, url, local_url));
Util.crashlyticsLog(Log.ERROR, TAG, "Unsuccessful download " + format(status, reason, title, url, local_url));
break;
}
} else {
Crashlytics.log(Log.ERROR, TAG, "Download database does not have an entry for " + format(status, reason, title, url, local_url));
Util.crashlyticsLog(Log.ERROR, TAG, "Download database does not have an entry for " + format(status, reason, title, url, local_url));
}
//} else {
// Download complete events may still come in, even after DownloadManager.remove is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import android.util.Log;
import android.widget.RemoteViews;

import com.crashlytics.android.Crashlytics;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -775,13 +774,13 @@ private void downloadSong(@NonNull Uri url, String title, @NonNull Uri serverUrl
.addRequestHeader("Authorization", "Basic " + base64EncodedCredentials);
long downloadId = downloadManager.enqueue(request);

Crashlytics.log("Registering new download");
Crashlytics.log("downloadId: " + downloadId);
Crashlytics.log("tempFile: " + tempFile);
Crashlytics.log("localPath: " + localPath);
Util.crashlyticsLog("Registering new download");
Util.crashlyticsLog("downloadId: " + downloadId);
Util.crashlyticsLog("tempFile: " + tempFile);
Util.crashlyticsLog("localPath: " + localPath);

if (!downloadDatabase.registerDownload(downloadId, tempFile, localPath)) {
Crashlytics.log(Log.WARN, TAG, "Could not register download entry for: " + downloadId);
Util.crashlyticsLog(Log.WARN, TAG, "Could not register download entry for: " + downloadId);
downloadManager.remove(downloadId);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.support.annotation.Nullable;
import android.util.Log;

import com.crashlytics.android.Crashlytics;
import com.google.common.annotations.VisibleForTesting;

import java.io.IOException;
Expand All @@ -16,6 +15,8 @@
import java.net.UnknownHostException;
import java.util.TreeMap;

import uk.org.ngo.squeezer.Util;

/**
* Scans the local network for servers.
*/
Expand Down Expand Up @@ -127,10 +128,10 @@ protected Void doInBackground(Void... unused) {
// new DatagramSocket(3483)
} catch (UnknownHostException e) {
// InetAddress.getByName()
Crashlytics.logException(e);
Util.crashlyticsLogException(e);
} catch (IOException e) {
// socket.send()
Crashlytics.logException(e);
Util.crashlyticsLogException(e);
}

if (socket != null) {
Expand Down

0 comments on commit 484ea34

Please sign in to comment.