Some changes to make app code less awful.

This commit is contained in:
Dmitry Isaenko 2019-07-26 00:55:44 +03:00
parent 86f2dd94f9
commit 6442ad8751
8 changed files with 74 additions and 64 deletions

View file

@ -7,8 +7,8 @@ android {
applicationId "com.blogspot.developersu.nowol.nowol" applicationId "com.blogspot.developersu.nowol.nowol"
minSdkVersion 14 minSdkVersion 14
targetSdkVersion 29 targetSdkVersion 29
versionCode 1 versionCode 2
versionName "1.0" versionName "2.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true vectorDrawables.useSupportLibrary = true
} }

View file

@ -13,7 +13,6 @@ import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
@ -21,20 +20,18 @@ import com.blogspot.developersu.nowol.nowol.com.blogspot.developersu.nowol.nowol
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
public class MainActivity extends AppCompatActivity implements popUp.pupUpRetuningValueListener { public class MainActivity extends AppCompatActivity implements popUp.pupUpRetuningValueListener {
private TextView hostAdress; private TextView hostAddress;
private TextView statusLbl; private TextView statusLbl;
private Button powerBtn;
private Button power5Btn;
private Button resetBtn;
Intent SendRequestIntent; Intent SendRequestIntent;
private SharedPreferences settings; private int status = -2;
SharedPreferences.Editor settingsEditor; SharedPreferences.Editor settingsEditor;
// define reciever for the data we got from service // define reciever for the data we got from service
private class MyResultReciever extends ResultReceiver{ private class MyResultReciever extends ResultReceiver{
public MyResultReciever(Handler handler) { MyResultReciever(Handler handler) {
super(handler); super(handler);
} }
@ -53,12 +50,13 @@ public class MainActivity extends AppCompatActivity implements popUp.pupUpRetuni
statusLbl.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorRed)); statusLbl.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorRed));
break; break;
case -1: case -1:
inform(getResources().getString(R.string.noResponse) + hostAdress.getText().toString()); inform(getResources().getString(R.string.noResponse) + hostAddress.getText().toString());
statusLbl.setText(getResources().getString(R.string.noResponse)); statusLbl.setText(getResources().getString(R.string.noResponse));
statusLbl.setTextColor(hostAdress.getTextColors()); statusLbl.setTextColor(hostAddress.getTextColors());
break; break;
default: break; default: break;
} }
status = resultCode;
} }
} }
//reciever end //reciever end
@ -82,6 +80,12 @@ public class MainActivity extends AppCompatActivity implements popUp.pupUpRetuni
return true; return true;
} }
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("STATE", status);
}
@Override @Override
protected void onCreate(final Bundle savedInstanceState) { protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -92,21 +96,43 @@ public class MainActivity extends AppCompatActivity implements popUp.pupUpRetuni
SendRequestIntent.putExtra("reciever", myReciever); SendRequestIntent.putExtra("reciever", myReciever);
// toolbar setup start // toolbar setup start
Toolbar toolBar = (Toolbar)findViewById(R.id.toolbar); Toolbar toolBar = findViewById(R.id.toolbar);
setSupportActionBar(toolBar); setSupportActionBar(toolBar);
final ActionBar ActBar = getSupportActionBar(); getSupportActionBar();
// toolbar setup end // toolbar setup end
hostAdress = (TextView)findViewById(R.id.hostNameStaticMain); hostAddress = findViewById(R.id.hostNameStaticMain);
statusLbl = (TextView)findViewById(R.id.statusTxtMainAct); statusLbl = findViewById(R.id.statusTxtMainAct);
powerBtn = (Button)findViewById(R.id.pwrBntMainAct);
power5Btn = (Button)findViewById(R.id.pwr5BntMainAct); if (savedInstanceState != null){
resetBtn = (Button)findViewById(R.id.resBntMainAct); status = savedInstanceState.getInt("STATE");
switch (status) {
case 1:
inform(getResources().getString(R.string.statusOnline));
statusLbl.setText(getResources().getString(R.string.statusOnline));
statusLbl.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark));
break;
case 0:
inform(getResources().getString(R.string.statusOffline));
statusLbl.setText(getResources().getString(R.string.statusOffline));
statusLbl.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorRed));
break;
case -1:
inform(getResources().getString(R.string.noResponse) + hostAddress.getText().toString());
statusLbl.setText(getResources().getString(R.string.noResponse));
statusLbl.setTextColor(hostAddress.getTextColors());
break;
}
}
Button powerBtn = findViewById(R.id.pwrBntMainAct);
Button power5Btn = findViewById(R.id.pwr5BntMainAct);
Button resetBtn = findViewById(R.id.resBntMainAct);
// Set request queue for Volley // Set request queue for Volley
settings = getSharedPreferences("NoWolPreferences", MODE_PRIVATE); SharedPreferences settings = getSharedPreferences("NoWolPreferences", MODE_PRIVATE);
hostAdress.setText(settings.getString("Host", getResources().getString(R.string.hostNameDefault))); hostAddress.setText(settings.getString("Host", getResources().getString(R.string.hostNameDefault)));
settingsEditor = settings.edit(); settingsEditor = settings.edit();
final Button.OnClickListener ActionButtonsListener = new Button.OnClickListener() { final Button.OnClickListener ActionButtonsListener = new Button.OnClickListener() {
@ -114,13 +140,13 @@ public class MainActivity extends AppCompatActivity implements popUp.pupUpRetuni
public void onClick(View v) { public void onClick(View v) {
switch (v.getId()) { switch (v.getId()) {
case R.id.pwrBntMainAct: case R.id.pwrBntMainAct:
doRequest(hostAdress.getText().toString() + "/?POWER0=on"); doRequest(hostAddress.getText().toString() + "/?POWER0=on");
break; break;
case R.id.pwr5BntMainAct: case R.id.pwr5BntMainAct:
doRequest(hostAdress.getText().toString() + "/?POWER1=on"); doRequest(hostAddress.getText().toString() + "/?POWER1=on");
break; break;
case R.id.resBntMainAct: case R.id.resBntMainAct:
doRequest(hostAdress.getText().toString() + "/?RESET=on"); doRequest(hostAddress.getText().toString() + "/?RESET=on");
break; break;
default: default:
break; break;
@ -131,11 +157,11 @@ public class MainActivity extends AppCompatActivity implements popUp.pupUpRetuni
powerBtn.setOnClickListener(ActionButtonsListener); powerBtn.setOnClickListener(ActionButtonsListener);
power5Btn.setOnClickListener(ActionButtonsListener); power5Btn.setOnClickListener(ActionButtonsListener);
resetBtn.setOnClickListener(ActionButtonsListener); resetBtn.setOnClickListener(ActionButtonsListener);
} }
@Override @Override
public void onFinishEdit (String hostNameReSet){ public void onFinishEdit (String hostNameReSet){
hostAdress.setText(hostNameReSet); hostAddress.setText(hostNameReSet);
inform(getResources().getString(R.string.hostLblMain) + hostNameReSet); inform(getResources().getString(R.string.hostLblMain) + hostNameReSet);
settingsEditor.putString("Host", hostNameReSet); settingsEditor.putString("Host", hostNameReSet);
settingsEditor.commit(); settingsEditor.commit();
@ -150,7 +176,7 @@ public class MainActivity extends AppCompatActivity implements popUp.pupUpRetuni
updateWidgetIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, IDs); updateWidgetIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, IDs);
getApplicationContext().sendBroadcast(updateWidgetIntent); getApplicationContext().sendBroadcast(updateWidgetIntent);
/* /*
broadcase end broadcast end
*/ */
} }
@ -158,10 +184,10 @@ public class MainActivity extends AppCompatActivity implements popUp.pupUpRetuni
public boolean onOptionsItemSelected(MenuItem item){ public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){ switch (item.getItemId()){
case R.id.refreshMenu: /* Button requests status */ case R.id.refreshMenu: /* Button requests status */
doRequest(hostAdress.getText().toString()); doRequest(hostAddress.getText().toString());
break; break;
case R.id.changeHostMenu: /* Button requests pop-up window */ case R.id.changeHostMenu: /* Button requests pop-up window */
popUp N = popUp.newInstance(hostAdress.getText()); popUp N = popUp.newInstance(hostAddress.getText());
N.show(this.getSupportFragmentManager(), "tst"); N.show(this.getSupportFragmentManager(), "tst");
break; break;
default: default:

View file

@ -17,8 +17,11 @@ import com.android.volley.toolbox.Volley;
public class SendRequestService extends IntentService { public class SendRequestService extends IntentService {
private RequestQueue queueStd; private static final int STATE_ON = 1;
private ResultReceiver resReciever; private static final int STATE_OFF = 0;
private static final int STATE_UNKNOWN = -1;
private ResultReceiver resReceiver;
public SendRequestService() { public SendRequestService() {
super("MyIntentService"); super("MyIntentService");
@ -51,7 +54,7 @@ public class SendRequestService extends IntentService {
} }
else{ else{
// Log.d("qwerty1212", "MainActivity case. Status = " + Integer.toString(state) + " awID = " + Integer.toString(awID)); // Log.d("qwerty1212", "MainActivity case. Status = " + Integer.toString(state) + " awID = " + Integer.toString(awID));
resReciever.send(state, null); resReceiver.send(state, null);
} }
} }
@ -61,30 +64,29 @@ public class SendRequestService extends IntentService {
// Log.d("service", "Got intent"); // Log.d("service", "Got intent");
Bundle bndle = intent.getExtras(); Bundle bndle = intent.getExtras();
String url = bndle.getString("url"); String url = bndle.getString("url");
resReciever = bndle.getParcelable("reciever"); resReceiver = bndle.getParcelable("reciever");
final int awID = bndle.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID); final int awID = bndle.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
// Log.d("qwerty1212", "got from " + Integer.toString(awID) +" "+ url); // Log.d("qwerty1212", "got from " + Integer.toString(awID) +" "+ url);
queueStd = Volley.newRequestQueue(this); RequestQueue queueStd = Volley.newRequestQueue(this);
StringRequest strRequest = new StringRequest(Request.Method.GET, url, //will be 4 requests StringRequest strRequest = new StringRequest(Request.Method.GET, url, //will be 4 requests
new Response.Listener<String>() { new Response.Listener<String>() {
@Override @Override
public void onResponse(String response) { public void onResponse(String response) {
if (response.contains("00c600")){ if (response.contains("00c600")){
sendData(1, awID); sendData(STATE_ON, awID);
} else { } else {
sendData(0, awID); sendData(STATE_OFF, awID);
} }
} }
}, new Response.ErrorListener() { }, new Response.ErrorListener() {
@Override @Override
public void onErrorResponse(VolleyError error) { public void onErrorResponse(VolleyError error) {
sendData(-1, awID); sendData(STATE_UNKNOWN, awID);
} }
}); });
queueStd.add(strRequest); queueStd.add(strRequest);
} }
} }
} }

View file

@ -16,16 +16,10 @@ import com.blogspot.developersu.nowol.nowol.SendRequestService;
public class NoWolWidget extends AppWidgetProvider { public class NoWolWidget extends AppWidgetProvider {
private SharedPreferences sharedSettings;
String hostIP;
int bgColor;
private void setRequests(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIDs){ private void setRequests(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIDs){
SharedPreferences sharedSettings = context.getSharedPreferences("NoWolPreferences", Context.MODE_PRIVATE);
sharedSettings = context.getSharedPreferences("NoWolPreferences", Context.MODE_PRIVATE); String hostIP = sharedSettings.getString("Host", context.getResources().getString(R.string.hostNameDefault));
hostIP = sharedSettings.getString("Host", context.getResources().getString(R.string.hostNameDefault)); int bgColor = sharedSettings.getInt("WidgetBgColor", Color.BLACK);
bgColor = sharedSettings.getInt("WidgetBgColor", Color.BLACK);
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.no_wol_widget); RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.no_wol_widget);

View file

@ -10,7 +10,6 @@ import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RemoteViews; import android.widget.RemoteViews;
import android.widget.SeekBar; import android.widget.SeekBar;
import android.widget.Switch; import android.widget.Switch;
@ -90,10 +89,10 @@ public class WidgetConfigurator extends AppCompatActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_widget_configurator); setContentView(R.layout.activity_widget_configurator);
Button submitBtn = (Button)findViewById(R.id.configBtnSubmit); Button submitBtn = findViewById(R.id.configBtnSubmit);
opacityBar = (SeekBar)findViewById(R.id.configSeekBar); opacityBar = findViewById(R.id.configSeekBar);
bkgoundSwitch = (Switch)findViewById(R.id.configSwitch); bkgoundSwitch = findViewById(R.id.configSwitch);
final TextView opacityLbl = (TextView)findViewById(R.id.configOpacityLbl); final TextView opacityLbl = findViewById(R.id.configOpacityLbl);
// Prepare seekBar element // Prepare seekBar element
opacityLbl.setText(getString(R.string.confOpacity) + " " + Integer.toString(opacityBar.getProgress())+"%"); opacityLbl.setText(getString(R.string.confOpacity) + " " + Integer.toString(opacityBar.getProgress())+"%");
@ -108,16 +107,7 @@ public class WidgetConfigurator extends AppCompatActivity {
@Override @Override
public void onStopTrackingTouch(SeekBar seekBar) {} public void onStopTrackingTouch(SeekBar seekBar) {}
}); });
// Prepare Switch colors element bkgoundSwitch.setText(getString(R.string.confWhite));
bkgoundSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked)
bkgoundSwitch.setText(getString(R.string.confWhite));
else
bkgoundSwitch.setText(getString(R.string.confBlack));;
}
});
// Cover widget intent // Cover widget intent
Intent initialConfIntent = getIntent(); Intent initialConfIntent = getIntent();

View file

@ -37,7 +37,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:text="@string/confBlack" /> android:text="@string/confWhite" />
<Space <Space
android:layout_width="match_parent" android:layout_width="match_parent"

View file

@ -25,6 +25,5 @@
<string name="confCreateWidget">"Создать виджет"</string> <string name="confCreateWidget">"Создать виджет"</string>
<string name="confOpacity">"Прозрачность"</string> <string name="confOpacity">"Прозрачность"</string>
<string name="confWhite">"Белый фон"</string> <string name="confWhite">"Белый фон"</string>
<string name="confBlack">"Чёрный фон"</string>
</resources> </resources>

View file

@ -25,6 +25,5 @@
<string name="confCreateWidget">"Create widget"</string> <string name="confCreateWidget">"Create widget"</string>
<string name="confOpacity">"Opacity"</string> <string name="confOpacity">"Opacity"</string>
<string name="confWhite">"White background"</string> <string name="confWhite">"White background"</string>
<string name="confBlack">"Black background"</string>
</resources> </resources>