HelloworldActivity.java
Go to the documentation of this file.
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;
48 
49  @Override
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
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 
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 
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 
109  private RunServerTask(HelloworldActivity activity) {
110  this.activityReference = new WeakReference<HelloworldActivity>(activity);
111  }
112 
113  @Override
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 
127  private GrpcTask(HelloworldActivity activity) {
128  this.activityReference = new WeakReference<HelloworldActivity>(activity);
129  }
130 
131  @Override
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
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 
157  public boolean isRunServerTaskCancelled() {
158  if (runServerTask != null) {
159  return runServerTask.isCancelled();
160  }
161  return false;
162  }
163 
164  public static native String sayHello(String host, int port, String message);
165 
166  public native void startServer(int port);
167 }
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
java.lang
io.grpc.helloworldexample.cpp.HelloworldActivity.messageEdit
EditText messageEdit
Definition: HelloworldActivity.java:43
io.grpc.helloworldexample.cpp.HelloworldActivity.onPause
void onPause()
Definition: HelloworldActivity.java:64
io.grpc.helloworldexample.cpp.HelloworldActivity.hostEdit
EditText hostEdit
Definition: HelloworldActivity.java:41
io.grpc.helloworldexample.cpp.HelloworldActivity.GrpcTask.doInBackground
String doInBackground(String... params)
Definition: HelloworldActivity.java:132
io.grpc.helloworldexample.cpp.HelloworldActivity.RunServerTask
Definition: HelloworldActivity.java:106
io.grpc.helloworldexample.cpp.HelloworldActivity.GrpcTask.GrpcTask
GrpcTask(HelloworldActivity activity)
Definition: HelloworldActivity.java:127
message
char * message
Definition: libuv/docs/code/tty-gravity/main.c:12
io.grpc.helloworldexample.cpp.HelloworldActivity.runServerTask
RunServerTask runServerTask
Definition: HelloworldActivity.java:47
io.grpc.helloworldexample.cpp.HelloworldActivity.sendMessage
void sendMessage(View view)
Definition: HelloworldActivity.java:77
io.grpc.helloworldexample.cpp.HelloworldActivity.GrpcTask
Definition: HelloworldActivity.java:124
io.grpc.helloworldexample.cpp.HelloworldActivity.serverPortEdit
EditText serverPortEdit
Definition: HelloworldActivity.java:44
io.grpc.helloworldexample.cpp.HelloworldActivity.sayHello
static native String sayHello(String host, int port, String message)
io.grpc.helloworldexample.cpp.HelloworldActivity.onCreate
void onCreate(Bundle savedInstanceState)
Definition: HelloworldActivity.java:50
io.grpc.helloworldexample.cpp.HelloworldActivity.startServer
native void startServer(int port)
io.grpc.helloworldexample.cpp.HelloworldActivity.RunServerTask.RunServerTask
RunServerTask(HelloworldActivity activity)
Definition: HelloworldActivity.java:109
tests.unit._exit_scenarios.port
port
Definition: _exit_scenarios.py:179
java
opencensus.proto.stats.v1.stats_pb2.View
View
Definition: stats_pb2.py:445
io.grpc.helloworldexample.cpp.HelloworldActivity.RunServerTask.doInBackground
Void doInBackground(Integer... params)
Definition: HelloworldActivity.java:114
io.grpc.helloworldexample.cpp.HelloworldActivity.serverButton
Button serverButton
Definition: HelloworldActivity.java:40
io.grpc.helloworldexample.cpp.HelloworldActivity.portEdit
EditText portEdit
Definition: HelloworldActivity.java:42
io.grpc.helloworldexample.cpp.HelloworldActivity.sendButton
Button sendButton
Definition: HelloworldActivity.java:39
io.grpc.helloworldexample.cpp.HelloworldActivity.isRunServerTaskCancelled
boolean isRunServerTaskCancelled()
Definition: HelloworldActivity.java:157
io.grpc.helloworldexample.cpp.HelloworldActivity
Definition: HelloworldActivity.java:33
io.grpc.helloworldexample.cpp.HelloworldActivity.grpcTask
GrpcTask grpcTask
Definition: HelloworldActivity.java:46
io.grpc.helloworldexample.cpp.HelloworldActivity.startOrStopServer
void startOrStopServer(View view)
Definition: HelloworldActivity.java:90
io.grpc.helloworldexample.cpp.HelloworldActivity.GrpcTask.activityReference
final WeakReference< HelloworldActivity > activityReference
Definition: HelloworldActivity.java:125
io.grpc.helloworldexample.cpp.HelloworldActivity.resultText
TextView resultText
Definition: HelloworldActivity.java:45
io.grpc.helloworldexample.cpp.HelloworldActivity.RunServerTask.activityReference
final WeakReference< HelloworldActivity > activityReference
Definition: HelloworldActivity.java:107
io.grpc.helloworldexample.cpp.HelloworldActivity.GrpcTask.onPostExecute
void onPostExecute(String result)
Definition: HelloworldActivity.java:141


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:12