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"
minSdkVersion 14
targetSdkVersion 29
versionCode 1
versionName "1.0"
versionCode 2
versionName "2.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}

View file

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

View file

@ -17,8 +17,11 @@ import com.android.volley.toolbox.Volley;
public class SendRequestService extends IntentService {
private RequestQueue queueStd;
private ResultReceiver resReciever;
private static final int STATE_ON = 1;
private static final int STATE_OFF = 0;
private static final int STATE_UNKNOWN = -1;
private ResultReceiver resReceiver;
public SendRequestService() {
super("MyIntentService");
@ -51,7 +54,7 @@ public class SendRequestService extends IntentService {
}
else{
// 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");
Bundle bndle = intent.getExtras();
String url = bndle.getString("url");
resReciever = bndle.getParcelable("reciever");
resReceiver = bndle.getParcelable("reciever");
final int awID = bndle.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
// 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
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.contains("00c600")){
sendData(1, awID);
sendData(STATE_ON, awID);
} else {
sendData(0, awID);
sendData(STATE_OFF, awID);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
sendData(-1, awID);
sendData(STATE_UNKNOWN, awID);
}
});
queueStd.add(strRequest);
}
}
}

View file

@ -16,16 +16,10 @@ import com.blogspot.developersu.nowol.nowol.SendRequestService;
public class NoWolWidget extends AppWidgetProvider {
private SharedPreferences sharedSettings;
String hostIP;
int bgColor;
private void setRequests(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIDs){
sharedSettings = context.getSharedPreferences("NoWolPreferences", Context.MODE_PRIVATE);
hostIP = sharedSettings.getString("Host", context.getResources().getString(R.string.hostNameDefault));
bgColor = sharedSettings.getInt("WidgetBgColor", Color.BLACK);
SharedPreferences sharedSettings = context.getSharedPreferences("NoWolPreferences", Context.MODE_PRIVATE);
String hostIP = sharedSettings.getString("Host", context.getResources().getString(R.string.hostNameDefault));
int bgColor = sharedSettings.getInt("WidgetBgColor", Color.BLACK);
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.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RemoteViews;
import android.widget.SeekBar;
import android.widget.Switch;
@ -90,10 +89,10 @@ public class WidgetConfigurator extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_widget_configurator);
Button submitBtn = (Button)findViewById(R.id.configBtnSubmit);
opacityBar = (SeekBar)findViewById(R.id.configSeekBar);
bkgoundSwitch = (Switch)findViewById(R.id.configSwitch);
final TextView opacityLbl = (TextView)findViewById(R.id.configOpacityLbl);
Button submitBtn = findViewById(R.id.configBtnSubmit);
opacityBar = findViewById(R.id.configSeekBar);
bkgoundSwitch = findViewById(R.id.configSwitch);
final TextView opacityLbl = findViewById(R.id.configOpacityLbl);
// Prepare seekBar element
opacityLbl.setText(getString(R.string.confOpacity) + " " + Integer.toString(opacityBar.getProgress())+"%");
@ -108,16 +107,7 @@ public class WidgetConfigurator extends AppCompatActivity {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
// Prepare Switch colors element
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));;
}
});
bkgoundSwitch.setText(getString(R.string.confWhite));
// Cover widget intent
Intent initialConfIntent = getIntent();

View file

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

View file

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

View file

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