• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018, gRPC Authors All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc.helloworldexample.cpp;
18 
19 import android.content.Context;
20 import android.os.AsyncTask;
21 import android.os.Bundle;
22 import android.support.v7.app.AppCompatActivity;
23 import android.text.TextUtils;
24 import android.text.method.ScrollingMovementMethod;
25 import android.view.View;
26 import android.view.inputmethod.InputMethodManager;
27 import android.widget.Button;
28 import android.widget.EditText;
29 import android.widget.TextView;
30 import android.widget.Toast;
31 import java.lang.ref.WeakReference;
32 
33 public class HelloworldActivity extends AppCompatActivity {
34 
35   static {
36     System.loadLibrary("grpc-helloworld");
37   }
38 
39   private Button sendButton;
40   private Button serverButton;
41   private EditText hostEdit;
42   private EditText portEdit;
43   private EditText messageEdit;
44   private EditText serverPortEdit;
45   private TextView resultText;
46   private GrpcTask grpcTask;
47   private RunServerTask runServerTask;
48 
49   @Override
onCreate(Bundle savedInstanceState)50   protected void onCreate(Bundle savedInstanceState) {
51     super.onCreate(savedInstanceState);
52     setContentView(R.layout.activity_helloworld);
53     sendButton = (Button) findViewById(R.id.send_button);
54     serverButton = (Button) findViewById(R.id.server_button);
55     hostEdit = (EditText) findViewById(R.id.host_edit_text);
56     portEdit = (EditText) findViewById(R.id.port_edit_text);
57     messageEdit = (EditText) findViewById(R.id.message_edit_text);
58     serverPortEdit = (EditText) findViewById(R.id.server_port_edit_text);
59     resultText = (TextView) findViewById(R.id.grpc_response_text);
60     resultText.setMovementMethod(new ScrollingMovementMethod());
61   }
62 
63   @Override
onPause()64   protected void onPause() {
65     super.onPause();
66     if (runServerTask != null) {
67       runServerTask.cancel(true);
68       runServerTask = null;
69       serverButton.setText("Start gRPC Server");
70     }
71     if (grpcTask != null) {
72       grpcTask.cancel(true);
73       grpcTask = null;
74     }
75   }
76 
sendMessage(View view)77   public void sendMessage(View view) {
78     ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
79         .hideSoftInputFromWindow(hostEdit.getWindowToken(), 0);
80     sendButton.setEnabled(false);
81     resultText.setText("");
82     grpcTask = new GrpcTask(this);
83     grpcTask.executeOnExecutor(
84         AsyncTask.THREAD_POOL_EXECUTOR,
85         hostEdit.getText().toString(),
86         messageEdit.getText().toString(),
87         portEdit.getText().toString());
88   }
89 
startOrStopServer(View view)90   public void startOrStopServer(View view) {
91     if (runServerTask != null) {
92       runServerTask.cancel(true);
93       runServerTask = null;
94       serverButton.setText("Start gRPC Server");
95       Toast.makeText(this, "Server stopped", Toast.LENGTH_SHORT).show();
96     } else {
97       runServerTask = new RunServerTask(this);
98       String portStr = serverPortEdit.getText().toString();
99       int port = TextUtils.isEmpty(portStr) ? 50051 : Integer.valueOf(portStr);
100       runServerTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, port);
101       serverButton.setText("Stop gRPC Server");
102       Toast.makeText(this, "Server started on port " + port, Toast.LENGTH_SHORT).show();
103     }
104   }
105 
106   private static class RunServerTask extends AsyncTask<Integer, Void, Void> {
107     private final WeakReference<HelloworldActivity> activityReference;
108 
RunServerTask(HelloworldActivity activity)109     private RunServerTask(HelloworldActivity activity) {
110       this.activityReference = new WeakReference<HelloworldActivity>(activity);
111     }
112 
113     @Override
doInBackground(Integer... params)114     protected Void doInBackground(Integer... params) {
115       int port = params[0];
116       HelloworldActivity activity = activityReference.get();
117       if (activity != null) {
118         activity.startServer(port);
119       }
120       return null;
121     }
122   }
123 
124   private static class GrpcTask extends AsyncTask<String, Void, String> {
125     private final WeakReference<HelloworldActivity> activityReference;
126 
GrpcTask(HelloworldActivity activity)127     private GrpcTask(HelloworldActivity activity) {
128       this.activityReference = new WeakReference<HelloworldActivity>(activity);
129     }
130 
131     @Override
doInBackground(String... params)132     protected String doInBackground(String... params) {
133       String host = params[0];
134       String message = params[1];
135       String portStr = params[2];
136       int port = TextUtils.isEmpty(portStr) ? 50051 : Integer.valueOf(portStr);
137       return sayHello(host, port, message);
138     }
139 
140     @Override
onPostExecute(String result)141     protected void onPostExecute(String result) {
142       HelloworldActivity activity = activityReference.get();
143       if (activity == null || isCancelled()) {
144         return;
145       }
146       TextView resultText = (TextView) activity.findViewById(R.id.grpc_response_text);
147       Button sendButton = (Button) activity.findViewById(R.id.send_button);
148       resultText.setText(result);
149       sendButton.setEnabled(true);
150     }
151   }
152 
153   /**
154    * Invoked by native code to stop server when RunServerTask has been canceled, either by user
155    * request or upon app moving to background.
156    */
isRunServerTaskCancelled()157   public boolean isRunServerTaskCancelled() {
158     if (runServerTask != null) {
159       return runServerTask.isCancelled();
160     }
161     return false;
162   }
163 
sayHello(String host, int port, String message)164   public static native String sayHello(String host, int port, String message);
165 
startServer(int port)166   public native void startServer(int port);
167 }
168