1616
1717package io .grpc .clientcacheexample ;
1818
19+ import android .app .Activity ;
1920import android .content .Context ;
2021import android .os .AsyncTask ;
2122import android .os .Bundle ;
4142import io .grpc .stub .ClientCalls ;
4243import java .io .PrintWriter ;
4344import java .io .StringWriter ;
45+ import java .lang .ref .WeakReference ;
4446import java .util .concurrent .TimeUnit ;
4547
4648public 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}
0 commit comments