Skip to content

Commit

Permalink
java: run clang-format for Java files (#56)
Browse files Browse the repository at this point in the history
`clang-format` can be used for auto-formatting Java files the same way it is for Objective-C and C++.

2 spaces were selected for spacing to maintain parity with upstream.

Depends on:
#7210
#7211

envoyproxy/envoy-mobile#55

Signed-off-by: Michael Rebello <[email protected]>
Signed-off-by: JP Simard <[email protected]>
  • Loading branch information
rebello95 authored and jpsim committed Nov 28, 2022
1 parent ed3ab8e commit 4e45738
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 135 deletions.
10 changes: 10 additions & 0 deletions mobile/.clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ PointerAlignment: Left
SortIncludes: false
...

---
Language: Java
AccessModifierOffset: -2
ColumnLimit: 100
DerivePointerAlignment: false
IndentWidth: 2
PointerAlignment: Left
SortIncludes: false
...

---
Language: ObjC
AccessModifierOffset: -2
Expand Down
2 changes: 1 addition & 1 deletion mobile/envoy
Submodule envoy updated from 71f1ab to f056e6
155 changes: 80 additions & 75 deletions mobile/examples/java/hello_world/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,92 +26,97 @@
import io.envoyproxy.envoymobile.Envoy;

public class MainActivity extends Activity {
private static final String ENDPOINT = "http://0.0.0.0:9001/api.lyft.com/static/demo/hello_world.txt";
private RecyclerView recyclerView;
private static final String ENDPOINT =
"http://0.0.0.0:9001/api.lyft.com/static/demo/hello_world.txt";
private RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Envoy envoy = new Envoy();
envoy.load();
String config = null;
try {
config = loadEnvoyConfig(getBaseContext(), R.raw.config);
} catch (RuntimeException e) {
Log.d("MainActivity", "exception getting config.", e);
throw new RuntimeException("Can't get config to run envoy.");
}
envoy.run(getBaseContext(), config);
Envoy envoy = new Envoy();
envoy.load();
String config = null;
try {
config = loadEnvoyConfig(getBaseContext(), R.raw.config);
} catch (RuntimeException e) {
Log.d("MainActivity", "exception getting config.", e);
throw new RuntimeException("Can't get config to run envoy.");
}
envoy.run(getBaseContext(), config);

recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

final ResponseRecyclerViewAdapter adapter = new ResponseRecyclerViewAdapter();
recyclerView.setAdapter(adapter);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
recyclerView.addItemDecoration(dividerItemDecoration);
HandlerThread thread = new HandlerThread("");
thread.start();
final Handler handler = new Handler(thread.getLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
try {
final Response response = makeRequest();
recyclerView.post((Runnable) () -> adapter.add(response));
} catch (IOException e) {
Log.d("MainActivity", "exception making request.", e);
}
// Make a call again
handler.postDelayed(this, TimeUnit.SECONDS.toMillis(1));
}
}, 0);
}
final ResponseRecyclerViewAdapter adapter = new ResponseRecyclerViewAdapter();
recyclerView.setAdapter(adapter);
DividerItemDecoration dividerItemDecoration =
new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
recyclerView.addItemDecoration(dividerItemDecoration);
HandlerThread thread = new HandlerThread("");
thread.start();

private Response makeRequest() throws IOException {
URL url = new URL(ENDPOINT);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int status = connection.getResponseCode();
if (status != 200) {
throw new IOException("non 200 status: " + status);
final Handler handler = new Handler(thread.getLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
try {
final Response response = makeRequest();
recyclerView.post((Runnable)() -> adapter.add(response));
} catch (IOException e) {
Log.d("MainActivity", "exception making request.", e);
}

List<String> serverHeaderField = connection.getHeaderFields().get("server");
InputStream inputStream = connection.getInputStream();
String body = deserialize(inputStream);
inputStream.close();
return new Response(body, serverHeaderField != null ? String.join(", ", serverHeaderField) : "");
// Make a call again
handler.postDelayed(this, TimeUnit.SECONDS.toMillis(1));
}
}, 0);
}

private Response makeRequest() throws IOException {
URL url = new URL(ENDPOINT);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
int status = connection.getResponseCode();
if (status != 200) {
throw new IOException("non 200 status: " + status);
}

private String deserialize(InputStream inputStream) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = bufferedReader.readLine();
while (line != null) {
stringBuilder.append(line);
line = bufferedReader.readLine();
}
bufferedReader.close();
return stringBuilder.toString();
List<String> serverHeaderField = connection.getHeaderFields().get("server");
InputStream inputStream = connection.getInputStream();
String body = deserialize(inputStream);
inputStream.close();
return new Response(body,
serverHeaderField != null ? String.join(", ", serverHeaderField) : "");
}

private String deserialize(InputStream inputStream) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = bufferedReader.readLine();
while (line != null) {
stringBuilder.append(line);
line = bufferedReader.readLine();
}
bufferedReader.close();
return stringBuilder.toString();
}

private String loadEnvoyConfig(Context context, int configResourceId) throws RuntimeException {
InputStream inputStream = context.getResources().openRawResource(configResourceId);
InputStreamReader inputReader = new InputStreamReader(inputStream);
BufferedReader bufReader = new BufferedReader(inputReader);
StringBuilder text = new StringBuilder();
private String loadEnvoyConfig(Context context, int configResourceId) throws RuntimeException {
InputStream inputStream = context.getResources().openRawResource(configResourceId);
InputStreamReader inputReader = new InputStreamReader(inputStream);
BufferedReader bufReader = new BufferedReader(inputReader);
StringBuilder text = new StringBuilder();

try {
String line;
while ((line = bufReader.readLine()) != null) {
text.append(line);
text.append('\n');
}
} catch (IOException e) {
return null;
}
return text.toString();
try {
String line;
while ((line = bufReader.readLine()) != null) {
text.append(line);
text.append('\n');
}
} catch (IOException e) {
return null;
}
return text.toString();
}
}
12 changes: 6 additions & 6 deletions mobile/examples/java/hello_world/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import java.util.List;

public class Response {
public final String title;
public final String header;
public final String title;
public final String header;

public Response(String title, String header) {
this.title = title;
this.header = header;
}
public Response(String title, String header) {
this.title = title;
this.header = header;
}
}
40 changes: 20 additions & 20 deletions mobile/examples/java/hello_world/ResponseRecyclerViewAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,28 @@
import java.util.List;

public class ResponseRecyclerViewAdapter extends RecyclerView.Adapter<ResponseViewHolder> {
private final List<Response> data = new ArrayList<>();
private final List<Response> data = new ArrayList<>();

@Override
public ResponseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.item, parent, false);
return new ResponseViewHolder(view);
}
@Override
public ResponseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.item, parent, false);
return new ResponseViewHolder(view);
}

@Override
public void onBindViewHolder(ResponseViewHolder holder, int position) {
holder.setResult(data.get(position));
}
@Override
public void onBindViewHolder(ResponseViewHolder holder, int position) {
holder.setResult(data.get(position));
}

@Override
public int getItemCount() {
return data.size();
}
@Override
public int getItemCount() {
return data.size();
}

public void add(Response response) {
data.add(0, response);
notifyItemInserted(0);
}
public void add(Response response) {
data.add(0, response);
notifyItemInserted(0);
}
}
25 changes: 13 additions & 12 deletions mobile/examples/java/hello_world/ResponseViewHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
import android.widget.TextView;

public class ResponseViewHolder extends RecyclerView.ViewHolder {
private final TextView responseTextView;
private final TextView headerTextView;
private final TextView responseTextView;
private final TextView headerTextView;

public ResponseViewHolder(View itemView) {
super(itemView);
this.responseTextView = (TextView) itemView.findViewById(R.id.response_text_view);
this.headerTextView = (TextView) itemView.findViewById(R.id.header_text_view);
}

public void setResult(Response response) {
responseTextView.setText(responseTextView.getResources().getString(R.string.title_string, response.title));
headerTextView.setText(headerTextView.getResources().getString(R.string.header_string, response.header));
}
public ResponseViewHolder(View itemView) {
super(itemView);
this.responseTextView = (TextView)itemView.findViewById(R.id.response_text_view);
this.headerTextView = (TextView)itemView.findViewById(R.id.header_text_view);
}

public void setResult(Response response) {
responseTextView.setText(
responseTextView.getResources().getString(R.string.title_string, response.title));
headerTextView.setText(
headerTextView.getResources().getString(R.string.header_string, response.header));
}
}
38 changes: 18 additions & 20 deletions mobile/library/java/io/envoyproxy/envoymobile/Envoy.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,22 @@

public class Envoy {

public void load() {
System.loadLibrary("envoy_jni");
}

public void run(Context context, String config) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
initialize((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
runEnvoy(config.trim());
}
});
thread.start();
}

private native int initialize(ConnectivityManager connectivityManager);

private native boolean isAresInitialized();

private native int runEnvoy(String config);
public void load() { System.loadLibrary("envoy_jni"); }

public void run(Context context, String config) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
initialize((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE));
runEnvoy(config.trim());
}
});
thread.start();
}

private native int initialize(ConnectivityManager connectivityManager);

private native boolean isAresInitialized();

private native int runEnvoy(String config);
}
3 changes: 2 additions & 1 deletion mobile/tools/check_format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ fi
# checker.
envoy/tools/check_format.py \
--add-excluded-prefixes ./envoy/ ./envoy_build_config/extensions_build_config.bzl ./WORKSPACE ./dist/Envoy.framework/ \
--skip_envoy_build_rule_check "$ENVOY_FORMAT_ACTION"
--skip_envoy_build_rule_check "$ENVOY_FORMAT_ACTION" \
--namespace_check_excluded_paths ./examples/ ./library/java/
envoy/tools/format_python_tools.sh check

0 comments on commit 4e45738

Please sign in to comment.