Skip to content

Commit 0373706

Browse files
authored
examples: update Android examples
Updates include: * Build file and dependency updates * Correcting the gradle wrapper for the clientcache example * Lint fixes (including making AsyncTask subclasses static) * Dropping the m-prefix from member variables * Fixing the code indentation * Fixing and enabling proguard for the routeguide example.
1 parent 4701932 commit 0373706

16 files changed

Lines changed: 536 additions & 479 deletions

File tree

examples/android/clientcache/app/build.gradle

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,28 @@ apply plugin: 'com.android.application'
22
apply plugin: 'com.google.protobuf'
33

44
android {
5-
compileSdkVersion 25
6-
buildToolsVersion "25.0.2"
5+
compileSdkVersion 27
76

87
defaultConfig {
98
applicationId "io.grpc.android.clientcacheexample"
109
minSdkVersion 19
11-
targetSdkVersion 25
10+
targetSdkVersion 27
1211
multiDexEnabled true
1312
versionCode 1
1413
versionName "1.0"
1514
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1615
}
1716
buildTypes {
17+
debug {
18+
minifyEnabled false
19+
}
1820
release {
1921
minifyEnabled true
2022
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
2123
}
2224
}
2325
lintOptions {
24-
disable 'InvalidPackage', 'HardcodedText'
26+
disable 'GoogleAppIndexingWarning', 'HardcodedText', 'InvalidPackage'
2527
textReport true
2628
textOutput "stdout"
2729
}
@@ -53,7 +55,7 @@ protobuf {
5355
}
5456

5557
dependencies {
56-
compile 'com.android.support:appcompat-v7:25.0.0'
58+
compile 'com.android.support:appcompat-v7:27.0.2'
5759

5860
// You need to build grpc-java to obtain these libraries below.
5961
compile 'io.grpc:grpc-okhttp:1.9.0-SNAPSHOT' // CURRENT_GRPC_VERSION
@@ -62,6 +64,6 @@ dependencies {
6264
compile 'javax.annotation:javax.annotation-api:1.2'
6365

6466
testCompile 'junit:junit:4.12'
65-
testCompile 'com.google.truth:truth:0.28'
67+
testCompile 'com.google.truth:truth:0.36'
6668
testCompile 'io.grpc:grpc-testing:1.9.0-SNAPSHOT'
6769
}

examples/android/clientcache/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<uses-permission android:name="android.permission.INTERNET" />
66

77
<application
8-
android:allowBackup="true"
8+
android:allowBackup="false"
99
android:icon="@mipmap/ic_launcher"
1010
android:label="@string/app_name"
1111
android:theme="@style/Base.V7.Theme.AppCompat.Light" >

examples/android/clientcache/app/src/main/java/io/grpc/clientcacheexample/ClientCacheExampleActivity.java

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.grpc.clientcacheexample;
1818

19+
import android.app.Activity;
1920
import android.content.Context;
2021
import android.os.AsyncTask;
2122
import android.os.Bundle;
@@ -41,16 +42,17 @@
4142
import io.grpc.stub.ClientCalls;
4243
import java.io.PrintWriter;
4344
import java.io.StringWriter;
45+
import java.lang.ref.WeakReference;
4446
import java.util.concurrent.TimeUnit;
4547

4648
public final class ClientCacheExampleActivity extends AppCompatActivity {
4749
private static final int CACHE_SIZE_IN_BYTES = 1 * 1024 * 1024; // 1MB
4850
private static final String TAG = "grpcCacheExample";
49-
private Button mSendButton;
50-
private EditText mHostEdit;
51-
private EditText mPortEdit;
52-
private EditText mMessageEdit;
53-
private TextView mResultText;
51+
private Button sendButton;
52+
private EditText hostEdit;
53+
private EditText portEdit;
54+
private EditText messageEdit;
55+
private TextView resultText;
5456
private CheckBox getCheckBox;
5557
private CheckBox noCacheCheckBox;
5658
private CheckBox onlyIfCachedCheckBox;
@@ -60,70 +62,79 @@ public final class ClientCacheExampleActivity extends AppCompatActivity {
6062
protected void onCreate(Bundle savedInstanceState) {
6163
super.onCreate(savedInstanceState);
6264
setContentView(R.layout.activity_clientcacheexample);
63-
mSendButton = (Button) findViewById(R.id.send_button);
64-
mHostEdit = (EditText) findViewById(R.id.host_edit_text);
65-
mPortEdit = (EditText) findViewById(R.id.port_edit_text);
66-
mMessageEdit = (EditText) findViewById(R.id.message_edit_text);
65+
sendButton = (Button) findViewById(R.id.send_button);
66+
hostEdit = (EditText) findViewById(R.id.host_edit_text);
67+
portEdit = (EditText) findViewById(R.id.port_edit_text);
68+
messageEdit = (EditText) findViewById(R.id.message_edit_text);
6769
getCheckBox = (CheckBox) findViewById(R.id.get_checkbox);
6870
noCacheCheckBox = (CheckBox) findViewById(R.id.no_cache_checkbox);
6971
onlyIfCachedCheckBox = (CheckBox) findViewById(R.id.only_if_cached_checkbox);
70-
mResultText = (TextView) findViewById(R.id.grpc_response_text);
71-
mResultText.setMovementMethod(new ScrollingMovementMethod());
72-
72+
resultText = (TextView) findViewById(R.id.grpc_response_text);
73+
resultText.setMovementMethod(new ScrollingMovementMethod());
7374
cache = SafeMethodCachingInterceptor.newLruCache(CACHE_SIZE_IN_BYTES);
7475
}
7576

7677
/** Sends RPC. Invoked when app button is pressed. */
7778
public void sendMessage(View view) {
7879
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
79-
.hideSoftInputFromWindow(mHostEdit.getWindowToken(), 0);
80-
mSendButton.setEnabled(false);
81-
new GrpcTask().execute();
80+
.hideSoftInputFromWindow(hostEdit.getWindowToken(), 0);
81+
sendButton.setEnabled(false);
82+
resultText.setText("");
83+
new GrpcTask(this, cache)
84+
.execute(
85+
hostEdit.getText().toString(),
86+
messageEdit.getText().toString(),
87+
portEdit.getText().toString(),
88+
getCheckBox.isChecked(),
89+
noCacheCheckBox.isChecked(),
90+
onlyIfCachedCheckBox.isChecked());
8291
}
8392

84-
private class GrpcTask extends AsyncTask<Void, Void, String> {
85-
private String host;
86-
private String message;
87-
private int port;
93+
private static class GrpcTask extends AsyncTask<Object, Void, String> {
94+
private final WeakReference<Activity> activityReference;
95+
private final SafeMethodCachingInterceptor.Cache cache;
8896
private ManagedChannel channel;
8997

90-
@Override
91-
protected void onPreExecute() {
92-
host = mHostEdit.getText().toString();
93-
message = mMessageEdit.getText().toString();
94-
String portStr = mPortEdit.getText().toString();
95-
port = TextUtils.isEmpty(portStr) ? 0 : Integer.valueOf(portStr);
96-
mResultText.setText("");
98+
private GrpcTask(Activity activity, SafeMethodCachingInterceptor.Cache cache) {
99+
this.activityReference = new WeakReference<Activity>(activity);
100+
this.cache = cache;
97101
}
98102

99103
@Override
100-
protected String doInBackground(Void... nothing) {
104+
protected String doInBackground(Object... params) {
105+
String host = (String) params[0];
106+
String message = (String) params[1];
107+
String portStr = (String) params[2];
108+
boolean useGet = (boolean) params[3];
109+
boolean noCache = (boolean) params[4];
110+
boolean onlyIfCached = (boolean) params[5];
111+
int port = TextUtils.isEmpty(portStr) ? 0 : Integer.valueOf(portStr);
101112
try {
102113
channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext(true).build();
103114
Channel channelToUse =
104115
ClientInterceptors.intercept(
105116
channel, SafeMethodCachingInterceptor.newSafeMethodCachingInterceptor(cache));
106-
HelloRequest message = HelloRequest.newBuilder().setName(this.message).build();
117+
HelloRequest request = HelloRequest.newBuilder().setName(message).build();
107118
HelloReply reply;
108-
if (getCheckBox.isChecked()) {
119+
if (useGet) {
109120
MethodDescriptor<HelloRequest, HelloReply> safeCacheableUnaryCallMethod =
110121
GreeterGrpc.getSayHelloMethod().toBuilder().setSafe(true).build();
111122
CallOptions callOptions = CallOptions.DEFAULT;
112-
if (noCacheCheckBox.isChecked()) {
123+
if (noCache) {
113124
callOptions =
114125
callOptions.withOption(SafeMethodCachingInterceptor.NO_CACHE_CALL_OPTION, true);
115126
}
116-
if (onlyIfCachedCheckBox.isChecked()) {
127+
if (onlyIfCached) {
117128
callOptions =
118129
callOptions.withOption(
119130
SafeMethodCachingInterceptor.ONLY_IF_CACHED_CALL_OPTION, true);
120131
}
121132
reply =
122133
ClientCalls.blockingUnaryCall(
123-
channelToUse, safeCacheableUnaryCallMethod, callOptions, message);
134+
channelToUse, safeCacheableUnaryCallMethod, callOptions, request);
124135
} else {
125136
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channelToUse);
126-
reply = stub.sayHello(message);
137+
reply = stub.sayHello(request);
127138
}
128139
return reply.getMessage();
129140
} catch (Exception e) {
@@ -145,8 +156,14 @@ protected void onPostExecute(String result) {
145156
Thread.currentThread().interrupt();
146157
}
147158
}
148-
mResultText.setText(result);
149-
mSendButton.setEnabled(true);
159+
Activity activity = activityReference.get();
160+
if (activity == null) {
161+
return;
162+
}
163+
TextView resultText = (TextView) activity.findViewById(R.id.grpc_response_text);
164+
Button sendButton = (Button) activity.findViewById(R.id.send_button);
165+
resultText.setText(result);
166+
sendButton.setEnabled(true);
150167
}
151168
}
152169
}

examples/android/clientcache/app/src/main/java/io/grpc/clientcacheexample/SafeMethodCachingInterceptor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import io.grpc.Metadata;
1515
import io.grpc.MethodDescriptor;
1616
import io.grpc.Status;
17+
import java.util.Locale;
1718
import java.util.Objects;
1819
import java.util.concurrent.TimeUnit;
1920

@@ -199,7 +200,7 @@ public void onHeaders(Metadata headers) {
199200
} else if (directive.equalsIgnoreCase("no-transform")) {
200201
cacheResponse = false;
201202
break;
202-
} else if (directive.toLowerCase().startsWith("max-age")) {
203+
} else if (directive.toLowerCase(Locale.US).startsWith("max-age")) {
203204
String[] parts = directive.split("=");
204205
if (parts.length == 2) {
205206
try {

examples/android/clientcache/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
buildscript {
44
repositories {
5+
google()
56
jcenter()
67
}
78
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.3.1'
9+
classpath 'com.android.tools.build:gradle:3.0.1'
910
classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.3"
1011

1112
// NOTE: Do not place your application dependencies here; they belong
@@ -15,6 +16,7 @@ buildscript {
1516

1617
allprojects {
1718
repositories {
19+
google()
1820
jcenter()
1921
mavenLocal()
2022
}

examples/android/helloworld/app/build.gradle

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,27 @@ apply plugin: 'com.android.application'
22
apply plugin: 'com.google.protobuf'
33

44
android {
5-
compileSdkVersion 22
6-
buildToolsVersion "25.0.2"
5+
compileSdkVersion 27
76

87
defaultConfig {
98
applicationId "io.grpc.android.helloworldexample"
109
// API level 14+ is required for TLS since Google Play Services v10.2
1110
minSdkVersion 14
12-
targetSdkVersion 22
11+
targetSdkVersion 27
1312
versionCode 1
1413
versionName "1.0"
1514
}
1615
buildTypes {
16+
debug {
17+
minifyEnabled false
18+
}
1719
release {
1820
minifyEnabled true
1921
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
2022
}
2123
}
2224
lintOptions {
23-
disable 'InvalidPackage', 'HardcodedText'
25+
disable 'GoogleAppIndexingWarning', 'HardcodedText', 'InvalidPackage'
2426
textReport true
2527
textOutput "stdout"
2628
}
@@ -52,7 +54,7 @@ protobuf {
5254
}
5355

5456
dependencies {
55-
compile 'com.android.support:appcompat-v7:22.1.1'
57+
compile 'com.android.support:appcompat-v7:27.0.2'
5658

5759
// You need to build grpc-java to obtain these libraries below.
5860
compile 'io.grpc:grpc-okhttp:1.10.0-SNAPSHOT' // CURRENT_GRPC_VERSION

examples/android/helloworld/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<uses-permission android:name="android.permission.INTERNET" />
66

77
<application
8-
android:allowBackup="true"
8+
android:allowBackup="false"
99
android:icon="@mipmap/ic_launcher"
1010
android:label="@string/app_name"
1111
android:theme="@style/Base.V7.Theme.AppCompat.Light" >

0 commit comments

Comments
 (0)