173 lines
4.6 KiB
Java
173 lines
4.6 KiB
Java
package net.droidtech.agct;
|
|
|
|
import android.app.Activity;
|
|
import android.app.ProgressDialog;
|
|
import android.content.ClipData;
|
|
import android.content.ClipboardManager;
|
|
import android.content.Context;
|
|
import android.os.Bundle;
|
|
import android.os.Handler;
|
|
import android.os.Message;
|
|
import android.view.View;
|
|
import android.widget.Button;
|
|
import android.widget.EditText;
|
|
import android.widget.Toast;
|
|
|
|
public class MainActivity extends Activity {
|
|
|
|
private ProgressDialog inProgress;
|
|
private EditText msgInput;
|
|
private EditText output;
|
|
private static ThreadResultHandler threadHandler;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_main);
|
|
|
|
inProgress=new ProgressDialog(this);
|
|
inProgress.setTitle(R.string.please_wait);
|
|
inProgress.setMessage(this.getApplicationContext().getResources().getString(R.string.in_progress));
|
|
inProgress.setCancelable(false);
|
|
|
|
threadHandler = new ThreadResultHandler();
|
|
;
|
|
this.msgInput=this.findViewById(R.id.messageInput);
|
|
this.output=this.findViewById(R.id.output);
|
|
|
|
output.setEnabled(false);
|
|
|
|
Button encode=this.findViewById(R.id.encode);
|
|
encode.setOnClickListener(new View.OnClickListener(){
|
|
|
|
@Override
|
|
public void onClick(View view){
|
|
|
|
EncodeThread thread=new EncodeThread();
|
|
|
|
try {
|
|
|
|
thread.putRawData(msgInput.getText().toString().getBytes("UTF-8"));
|
|
|
|
}catch (Exception e){
|
|
|
|
Toast.makeText(MainActivity.this,R.string.encoding_error,Toast.LENGTH_LONG).show();
|
|
|
|
return;
|
|
|
|
}
|
|
thread.setHandler(threadHandler);
|
|
inProgress.show();
|
|
thread.start();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Button decode=findViewById(R.id.decode);
|
|
decode.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
|
|
DecodeThread thread=new DecodeThread();
|
|
thread.setHandler(threadHandler);
|
|
|
|
if(msgInput.getText().toString().trim().isEmpty()){
|
|
|
|
String first=((ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE)).getPrimaryClip().getItemAt(0).getText().toString();
|
|
|
|
Toast.makeText(MainActivity.this,R.string.read_clipboard,Toast.LENGTH_LONG).show();
|
|
thread.putEncodedData(first);
|
|
|
|
}else {
|
|
|
|
thread.putEncodedData(msgInput.getText().toString());
|
|
|
|
}
|
|
|
|
inProgress.show();
|
|
thread.start();
|
|
|
|
}
|
|
});
|
|
|
|
Button copy_output=findViewById(R.id.copy_output);
|
|
copy_output.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
|
|
if(threadHandler.lastOutputMessage!=null){
|
|
|
|
ClipboardManager cm =(ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
|
|
|
|
ClipData cd = ClipData.newPlainText("output",threadHandler.lastOutputMessage);
|
|
|
|
cm.setPrimaryClip(cd);
|
|
|
|
Toast.makeText(MainActivity.this,R.string.copied,Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
private class ThreadResultHandler extends Handler{
|
|
|
|
private String lastOutputMessage=null;
|
|
|
|
@Override
|
|
public void handleMessage(Message msg){
|
|
try {
|
|
|
|
if (msg.what == 2) {
|
|
|
|
output.setEnabled(false);
|
|
|
|
if(((String)msg.obj).length()>1024){
|
|
|
|
output.setText(MainActivity.this.getResources().getText(R.string.message_too_long));
|
|
|
|
}else {
|
|
|
|
output.setText((String) msg.obj);
|
|
|
|
}
|
|
|
|
lastOutputMessage=(String)msg.obj;
|
|
|
|
} else if (msg.what == 4) {
|
|
|
|
output.setEnabled(true);
|
|
|
|
output.setText(new String(((byte[]) msg.obj), "UTF-8"));
|
|
|
|
this.lastOutputMessage=new String(((byte[]) msg.obj), "UTF-8");
|
|
|
|
}
|
|
|
|
}catch(Exception e){
|
|
|
|
output.setText(MainActivity.this.getResources().getString(R.string.error));
|
|
output.setEnabled(false);
|
|
|
|
}
|
|
|
|
if(msg.what==1) {
|
|
|
|
output.setText(R.string.error);
|
|
lastOutputMessage=MainActivity.this.getString(R.string.error);
|
|
output.setEnabled(false);
|
|
|
|
}
|
|
|
|
inProgress.dismiss();
|
|
}
|
|
}
|
|
|
|
}
|