33import android .app .Activity ;
44import android .content .Context ;
55import android .content .Intent ;
6+ import android .content .pm .ActivityInfo ;
7+ import android .content .res .Configuration ;
68import android .os .Bundle ;
79import android .text .TextUtils ;
810import android .view .View ;
11+ import android .view .Window ;
912import android .widget .TextView ;
1013
1114import com .koushikdutta .async .AsyncServer ;
@@ -41,51 +44,89 @@ public static Intent createIntent(Context context) {
4144 private Activity context ;
4245 private boolean isAlive ;
4346
44- private TextView etUnitPort ;
4547 private TextView tvUnitRequest ;
4648 private TextView tvUnitResponse ;
49+
50+ private TextView tvUnitOrient ;
51+ private TextView etUnitPort ;
52+ private View pbUnit ;
4753 @ Override
4854 protected void onCreate (Bundle savedInstanceState ) {
4955 super .onCreate (savedInstanceState );
56+ requestWindowFeature (Window .FEATURE_NO_TITLE );
5057 setContentView (R .layout .unit_activity );
5158 context = this ;
5259 isAlive = true ;
5360
54- etUnitPort = findViewById ( R . id . etUnitPort );
61+
5562 tvUnitRequest = findViewById (R .id .tvUnitRequest );
5663 tvUnitResponse = findViewById (R .id .tvUnitResponse );
64+
65+ tvUnitOrient = findViewById (R .id .tvUnitOrient );
66+ etUnitPort = findViewById (R .id .etUnitPort );
67+ pbUnit = findViewById (R .id .pbUnit );
68+
5769 etUnitPort .setText (port );
70+ pbUnit .setVisibility (View .GONE );
71+
72+
73+ getWindow ().getDecorView ().addOnLayoutChangeListener (new View .OnLayoutChangeListener () {
74+ @ Override
75+ public void onLayoutChange (View v , int left , int top , int right , int bottom , int oldLeft , int oldTop , int oldRight , int oldBottom ) {
76+ onConfigurationChanged (getResources ().getConfiguration ());
77+ }
78+ });
79+
80+ }
81+
82+ @ Override
83+ protected void onResume () {
84+ super .onResume ();
85+ onConfigurationChanged (getResources ().getConfiguration ());
86+ }
87+
88+ @ Override
89+ public void onConfigurationChanged (Configuration newConfig ) {
90+ tvUnitOrient .setText (
91+ getResources ().getConfiguration ().orientation == Configuration .ORIENTATION_LANDSCAPE
92+ ? (getString (R .string .screen ) + getString (R .string .horizontal ))
93+ : getString (R .string .vertical )
94+ );
95+ super .onConfigurationChanged (newConfig );
96+ }
97+
98+ public void copy (View v ) {
99+ StringUtil .copyText (context , StringUtil .getString ((TextView ) v ));
100+ }
101+
102+ public void orient (View v ) {
103+ setRequestedOrientation (getResources ().getConfiguration ().orientation == Configuration .ORIENTATION_LANDSCAPE
104+ ? ActivityInfo .SCREEN_ORIENTATION_USER_PORTRAIT : ActivityInfo .SCREEN_ORIENTATION_USER_LANDSCAPE );
58105 }
59106
60107
61108 private String port = "8080" ;
62109 public void start (View v ) {
63110 v .setEnabled (false );
64111 port = StringUtil .getString (etUnitPort );
112+ startServer (Integer .valueOf (port ));
65113
66- etUnitPort .setText (port + " is starting..." );
67- startServer ();
68-
69- etUnitPort .setText (port + " is listening..." );
70- etUnitPort .setEnabled (false );
71114 v .setEnabled (true );
115+ etUnitPort .setEnabled (false );
116+ pbUnit .setVisibility (View .VISIBLE );
72117 }
73118 public void stop (View v ) {
74119 v .setEnabled (false );
75120 server .stop ();
76121 mAsyncServer .stop ();
77122
78- etUnitPort .setText (port );
79- etUnitPort .setEnabled (true );
80123 v .setEnabled (true );
124+ etUnitPort .setEnabled (true );
125+ pbUnit .setVisibility (View .GONE );
81126 }
82127
83128
84- public void copy (View v ) {
85- StringUtil .copyText (context , StringUtil .getString ((TextView ) v ));
86- }
87-
88- private void startServer () {
129+ private void startServer (int port ) {
89130// server.addAction("OPTIONS","*", this);
90131// server.get("/test", new HttpServerRequestCallback() {
91132// @Override
@@ -99,7 +140,7 @@ private void startServer() {
99140 server .get ("[\\ d\\ D]*" , this );
100141// server.post("/get", this);
101142 server .post ("[\\ d\\ D]*" , this );
102- server .listen (mAsyncServer , Integer . valueOf ( port ) );
143+ server .listen (mAsyncServer , port );
103144
104145 }
105146
0 commit comments