EmojiCompat: Several fixes

Update dependencies. The minSdkVersion is now 19.

InitCallback in MainActivity no longer hold a hard reference to the
Context.

Test: Manually on API Level 26 and 19
Change-Id: Ie51d2b56c8a8884b48fc9e867c76677a98c24dbc
diff --git a/views/EmojiCompat/app/build.gradle b/views/EmojiCompat/app/build.gradle
index fec4dab..4867afc 100644
--- a/views/EmojiCompat/app/build.gradle
+++ b/views/EmojiCompat/app/build.gradle
@@ -17,16 +17,16 @@
 apply plugin: 'com.android.application'
 
 ext {
-    supportLibVersion = '26.0.0-beta1'
+    supportLibVersion = '26.0.0'
 }
 
 android {
-    compileSdkVersion 'android-O'
-    buildToolsVersion "26.0.0-rc1"
+    compileSdkVersion 26
+    buildToolsVersion "26.0.0"
     defaultConfig {
         applicationId "com.example.android.emojicompat"
-        minSdkVersion 'O'
-        targetSdkVersion 'O'
+        minSdkVersion 19
+        targetSdkVersion 26
         versionCode 1
         versionName "1.0"
         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
@@ -51,5 +51,5 @@
 
     // Test
     testCompile 'junit:junit:4.12'
-    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
+    androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.0'
 }
diff --git a/views/EmojiCompat/app/src/main/java/com/example/android/emojicompat/MainActivity.java b/views/EmojiCompat/app/src/main/java/com/example/android/emojicompat/MainActivity.java
index 2ef3702..2f93c54 100644
--- a/views/EmojiCompat/app/src/main/java/com/example/android/emojicompat/MainActivity.java
+++ b/views/EmojiCompat/app/src/main/java/com/example/android/emojicompat/MainActivity.java
@@ -16,11 +16,14 @@
 
 package com.example.android.emojicompat;
 
+import android.content.Context;
 import android.os.Bundle;
 import android.support.text.emoji.EmojiCompat;
 import android.support.v7.app.AppCompatActivity;
 import android.widget.TextView;
 
+import java.lang.ref.WeakReference;
+
 
 public class MainActivity extends AppCompatActivity {
 
@@ -51,18 +54,32 @@
 
         // Regular TextView without EmojiCompat support; you have to manually process the text
         final TextView regularTextView = findViewById(R.id.regular_text_view);
-        EmojiCompat.get().registerInitCallback(new EmojiCompat.InitCallback() {
-            @Override
-            public void onInitialized() {
-                final EmojiCompat compat = EmojiCompat.get();
-                regularTextView.setText(
-                        compat.process(getString(R.string.regular_text_view, EMOJI)));
-            }
-        });
+        EmojiCompat.get().registerInitCallback(new InitCallback(regularTextView));
 
         // Custom TextView
         final TextView customTextView = findViewById(R.id.emoji_custom_text_view);
         customTextView.setText(getString(R.string.custom_text_view, EMOJI));
     }
 
+    private static class InitCallback extends EmojiCompat.InitCallback {
+
+        private final WeakReference<TextView> mRegularTextViewRef;
+
+        InitCallback(TextView regularTextView) {
+            mRegularTextViewRef = new WeakReference<>(regularTextView);
+        }
+
+        @Override
+        public void onInitialized() {
+            final TextView regularTextView = mRegularTextViewRef.get();
+            if (regularTextView != null) {
+                final EmojiCompat compat = EmojiCompat.get();
+                final Context context = regularTextView.getContext();
+                regularTextView.setText(
+                        compat.process(context.getString(R.string.regular_text_view, EMOJI)));
+            }
+        }
+
+    }
+
 }
diff --git a/views/EmojiCompat/build.gradle b/views/EmojiCompat/build.gradle
index cb27953..5811ab0 100644
--- a/views/EmojiCompat/build.gradle
+++ b/views/EmojiCompat/build.gradle
@@ -19,14 +19,14 @@
         jcenter()
     }
     dependencies {
-        classpath 'com.android.tools.build:gradle:2.4.0-alpha6'
+        classpath 'com.android.tools.build:gradle:2.3.3'
     }
 }
 
 allprojects {
     repositories {
         jcenter()
-        maven { url 'https://maven.google.com' }
+        google()
     }
 }
 
diff --git a/views/EmojiCompat/gradle/wrapper/gradle-wrapper.properties b/views/EmojiCompat/gradle/wrapper/gradle-wrapper.properties
index dbde6ba..93a1579 100644
--- a/views/EmojiCompat/gradle/wrapper/gradle-wrapper.properties
+++ b/views/EmojiCompat/gradle/wrapper/gradle-wrapper.properties
@@ -3,4 +3,4 @@
 distributionPath=wrapper/dists
 zipStoreBase=GRADLE_USER_HOME
 zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-4.0.2-all.zip
diff --git a/views/EmojiCompat/kotlinApp/app/build.gradle b/views/EmojiCompat/kotlinApp/app/build.gradle
index af061cd..de2074b 100644
--- a/views/EmojiCompat/kotlinApp/app/build.gradle
+++ b/views/EmojiCompat/kotlinApp/app/build.gradle
@@ -16,7 +16,7 @@
 
 buildscript {
     ext {
-        kotlin_version = '1.1.2-3'
+        kotlin_version = '1.1.3-2'
     }
     repositories {
         jcenter()
@@ -30,16 +30,16 @@
 apply plugin: 'kotlin-android'
 
 ext {
-    supportLibVersion = '26.0.0-beta1'
+    supportLibVersion = '26.0.0'
 }
 
 android {
-    compileSdkVersion 'android-O'
-    buildToolsVersion "26.0.0-rc1"
+    compileSdkVersion 26
+    buildToolsVersion '26.0.0'
     defaultConfig {
         applicationId "com.example.android.emojicompat"
-        minSdkVersion 'O'
-        targetSdkVersion 'O'
+        minSdkVersion 19
+        targetSdkVersion 26
         versionCode 1
         versionName "1.0"
         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
diff --git a/views/EmojiCompat/kotlinApp/app/src/main/java/com/example/android/emojicompat/MainActivity.kt b/views/EmojiCompat/kotlinApp/app/src/main/java/com/example/android/emojicompat/MainActivity.kt
index 7cc9033..b23c800 100644
--- a/views/EmojiCompat/kotlinApp/app/src/main/java/com/example/android/emojicompat/MainActivity.kt
+++ b/views/EmojiCompat/kotlinApp/app/src/main/java/com/example/android/emojicompat/MainActivity.kt
@@ -20,6 +20,7 @@
 import android.support.text.emoji.EmojiCompat
 import android.support.v7.app.AppCompatActivity
 import android.widget.TextView
+import java.lang.ref.WeakReference
 
 
 class MainActivity : AppCompatActivity() {
@@ -53,16 +54,27 @@
 
         // Regular TextView without EmojiCompat support; you have to manually process the text
         val regularTextView: TextView = findViewById(R.id.regular_text_view)
-        EmojiCompat.get().registerInitCallback(object : EmojiCompat.InitCallback() {
-            override fun onInitialized() {
-                val compat = EmojiCompat.get()
-                regularTextView.text = compat.process(getString(R.string.regular_text_view, EMOJI))
-            }
-        })
+        EmojiCompat.get().registerInitCallback(InitCallback(regularTextView))
 
         // Custom TextView
         val customTextView: TextView = findViewById(R.id.emoji_custom_text_view)
         customTextView.text = getString(R.string.custom_text_view, EMOJI)
     }
 
+    private class InitCallback(regularTextView: TextView) : EmojiCompat.InitCallback() {
+
+        val regularTextViewRef = WeakReference(regularTextView)
+
+        override fun onInitialized() {
+            val regularTextView = regularTextViewRef.get()
+            if (regularTextView != null) {
+                val compat = EmojiCompat.get()
+                val context = regularTextView.context
+                regularTextView.text = compat.process(
+                        context.getString(R.string.regular_text_view, EMOJI))
+            }
+        }
+
+    }
+
 }
diff --git a/views/EmojiCompat/kotlinApp/build.gradle b/views/EmojiCompat/kotlinApp/build.gradle
index 706e32e..f25c0d2 100644
--- a/views/EmojiCompat/kotlinApp/build.gradle
+++ b/views/EmojiCompat/kotlinApp/build.gradle
@@ -19,14 +19,14 @@
         jcenter()
     }
     dependencies {
-        classpath 'com.android.tools.build:gradle:2.4.0-alpha6'
+        classpath 'com.android.tools.build:gradle:2.3.3'
     }
 }
 
 allprojects {
     repositories {
         jcenter()
-        maven { url 'https://maven.google.com' }
+        google()
     }
 }
 
diff --git a/views/EmojiCompat/kotlinApp/gradle/wrapper/gradle-wrapper.properties b/views/EmojiCompat/kotlinApp/gradle/wrapper/gradle-wrapper.properties
index dbde6ba..93a1579 100644
--- a/views/EmojiCompat/kotlinApp/gradle/wrapper/gradle-wrapper.properties
+++ b/views/EmojiCompat/kotlinApp/gradle/wrapper/gradle-wrapper.properties
@@ -3,4 +3,4 @@
 distributionPath=wrapper/dists
 zipStoreBase=GRADLE_USER_HOME
 zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-4.0.2-all.zip