Skip to content

Commit

Permalink
UI improvement & screen rotation support
Browse files Browse the repository at this point in the history
  • Loading branch information
Viknet committed Jun 11, 2017
1 parent 44ffde7 commit 76d321a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 32 deletions.
1 change: 1 addition & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
android:allowBackup="true" >
<activity
android:name="com.viknet.cnping.MainActivity"
android:configChanges="orientation|screenSize"
android:label="com.viknet.cnping" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ If you are android developer and have some time, I would be glad to hear some wo

## TODO
- [ ] UI fool proofing
- [ ] View resize support (including orientation change)
- [x] View resize support (including orientation change)
- [ ] Parameters (packet size, ping period)
- [ ] Performance fixes
- [ ] Testing on different devices
13 changes: 4 additions & 9 deletions res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight = "0"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="horizontal">

<EditText
Expand All @@ -23,21 +25,14 @@
android:layout_height="wrap_content"
android:layout_weight = "1"
android:singleLine="true"
android:autoText="false"
android:typeface="monospace"
android:imeOptions="actionDone"
android:text="google.com"/>

<Button
android:id="@+id/goButton"
android:text="GO"
android:layout_width="10mm"
android:layout_height="wrap_content"
android:layout_weight = "0"/>

<Button
android:id="@+id/stopButton"
android:text="Stop"
android:layout_width="10mm"
android:layout_width="15mm"
android:layout_height="wrap_content"
android:layout_weight = "0"/>
</LinearLayout>
Expand Down
87 changes: 66 additions & 21 deletions src/com/viknet/cnping/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.widget.EditText;
import android.widget.TextView;
import android.content.Context;
import android.widget.Button;
import android.view.View;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.util.Log;
import android.os.AsyncTask;
import android.view.inputmethod.InputMethodManager;

public class MainActivity extends Activity {

Expand All @@ -18,45 +23,85 @@ public class MainActivity extends Activity {
}

private class SuSocketsRequest extends AsyncTask<Void, Void, Boolean> {
protected Boolean doInBackground(Void... params) {
return requestSockets();
}
protected void onPostExecute(Boolean result) {
if (result) return;
protected Boolean doInBackground(Void... params) {
return requestSockets();
}
protected void onPostExecute(Boolean result) {
if (result) return;

new AlertDialog.Builder(MainActivity.this).setTitle("Error").setMessage("Cannot get sockets with su.").setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}}).create().show();
}
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}}).create().show();
}
}

private EditText tv;
private Button goButton;
private static native boolean requestSockets();
private static native boolean startPing(String hostname);
private static native void stopPing();
private boolean isRunning = false;

protected void hideKeyboard(){
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(tv.getWindowToken(), 0);
tv.clearFocus();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// findViewById(R.id.imageView).setBackgroundColor(0xFF707070);
tv = (EditText) findViewById(R.id.textHostname);
new SuSocketsRequest().execute();
getActionBar().setTitle("CNPing");
setContentView(R.layout.activity_main);

//TODO should be one button switching action
((Button)findViewById(R.id.goButton)).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// tv.setText(startPing()?"Ping success":"Ping fail");
startPing(tv.getText().toString());
tv = (EditText) findViewById(R.id.textHostname);
goButton = ((Button)findViewById(R.id.goButton));
new SuSocketsRequest().execute();

tv.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE))
hideKeyboard();
return false;
}
});
((Button)findViewById(R.id.stopButton)).setOnClickListener(new View.OnClickListener() {

goButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
findViewById(R.id.imageView).postInvalidate();
stopPing();
if(!isRunning){
if (startPing(tv.getText().toString())){
hideKeyboard();
((Button)v).setText("STOP");
isRunning = true;
} else {
new AlertDialog.Builder(MainActivity.this).setTitle("Error").setMessage("Ping error.").setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}}).create().show();
}
} else {
stopPing();
((Button)v).setText("GO");
isRunning = false;
}
}
});
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

getActionBar().show();
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
getActionBar().hide();
((View)findViewById(R.id.controlsArea)).setVisibility(View.GONE);
} else {
getActionBar().show();
((View)findViewById(R.id.controlsArea)).setVisibility(View.VISIBLE);
}
}
}
1 change: 0 additions & 1 deletion src/com/viknet/cnping/PingImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ protected void onDraw(Canvas canvas) {
}

protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
//FIXME changing size not working somehow
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int newHeight= MeasureSpec.getSize(heightMeasureSpec);
final int newWidth= MeasureSpec.getSize(widthMeasureSpec);
Expand Down

0 comments on commit 76d321a

Please sign in to comment.