2018-11-25 11:54:36 +01:00
|
|
|
package org.disroot.disrootapp.ui;
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
import android.app.ProgressDialog;
|
2018-12-09 18:42:42 +01:00
|
|
|
import android.content.Intent;
|
2018-11-25 22:39:04 +01:00
|
|
|
import android.graphics.Color;
|
2018-11-25 11:54:36 +01:00
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
import android.support.v7.widget.Toolbar;
|
|
|
|
import android.util.Log;
|
2018-12-09 18:42:42 +01:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2018-11-25 11:54:36 +01:00
|
|
|
import android.view.View;
|
2018-11-25 22:39:04 +01:00
|
|
|
import android.view.ViewGroup;
|
2018-12-09 18:42:42 +01:00
|
|
|
import android.widget.Button;
|
2018-11-25 11:54:36 +01:00
|
|
|
import android.widget.ListAdapter;
|
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.SimpleAdapter;
|
2018-11-25 22:39:04 +01:00
|
|
|
import android.widget.TextView;
|
2018-11-25 11:54:36 +01:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import org.disroot.disrootapp.R;
|
|
|
|
import org.disroot.disrootapp.utils.HttpHandler;
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
public class StateActivity extends AppCompatActivity {
|
|
|
|
|
2018-12-09 18:42:42 +01:00
|
|
|
Button button;
|
|
|
|
|
2022-10-29 02:01:02 +02:00
|
|
|
private final String TAG = StateActivity.class.getSimpleName();
|
2018-11-25 11:54:36 +01:00
|
|
|
|
|
|
|
private ProgressDialog pDialog;
|
|
|
|
private ListView lv;
|
|
|
|
|
|
|
|
// URL to get data JSON
|
2022-10-28 20:52:08 +02:00
|
|
|
static String url = "https://status.disroot.org/index.json";
|
2018-11-25 11:54:36 +01:00
|
|
|
|
|
|
|
ArrayList<HashMap<String, String>> stateList;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_state);
|
|
|
|
|
|
|
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
|
|
|
setSupportActionBar(toolbar);
|
|
|
|
toolbar.setNavigationIcon(R.drawable.ic_arrow_back);
|
2022-10-29 02:01:02 +02:00
|
|
|
toolbar.setNavigationOnClickListener( v -> onBackPressed() );
|
2018-12-09 18:42:42 +01:00
|
|
|
|
|
|
|
button = findViewById(R.id.StateMessageBtn);//StateMessageBtn
|
2022-10-29 02:01:02 +02:00
|
|
|
button.setOnClickListener( arg0 -> {
|
|
|
|
Intent goState = new Intent(StateActivity.this, StateMessagesActivity.class);
|
|
|
|
StateActivity.this.startActivity(goState);
|
|
|
|
} );
|
2018-12-09 18:42:42 +01:00
|
|
|
|
2018-11-25 11:54:36 +01:00
|
|
|
stateList = new ArrayList<>();
|
|
|
|
|
|
|
|
lv = findViewById(R.id.list);
|
|
|
|
|
|
|
|
new GetList().execute();
|
|
|
|
}
|
|
|
|
|
2018-12-09 18:42:42 +01:00
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
// Inflate the menu; this adds items to the action bar if it is present.
|
|
|
|
getMenuInflater().inflate(R.menu.menu_about, menu);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
// Handle action bar item clicks here. The action bar will
|
|
|
|
// automatically handle clicks on the Home/Up button, so long
|
|
|
|
// as you specify a parent activity in AndroidManifest.xml.
|
|
|
|
int id = item.getItemId();
|
|
|
|
|
|
|
|
|
|
|
|
//noinspection SimplifiableIfStatement
|
|
|
|
if (id == R.id.action_home) {
|
|
|
|
Intent goHome = new Intent(StateActivity.this, MainActivity.class);
|
|
|
|
StateActivity.this.startActivity(goHome);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
2018-11-25 11:54:36 +01:00
|
|
|
/**
|
|
|
|
* Async task class to get json by making HTTP call
|
|
|
|
*/
|
|
|
|
@SuppressLint("StaticFieldLeak")
|
|
|
|
class GetList extends AsyncTask<Void, Void, Void> {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPreExecute() {
|
|
|
|
super.onPreExecute();
|
|
|
|
// Showing progress dialog
|
|
|
|
pDialog = new ProgressDialog(StateActivity.this);
|
2018-11-27 01:43:42 +01:00
|
|
|
pDialog.setMessage("Loading…");
|
2018-11-25 11:54:36 +01:00
|
|
|
pDialog.setCancelable(false);
|
|
|
|
pDialog.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... arg0) {
|
|
|
|
HttpHandler sh = new HttpHandler();
|
|
|
|
|
|
|
|
// Making a request to url and getting response
|
|
|
|
String jsonStr0 = sh.makeServiceCall(url);
|
|
|
|
|
|
|
|
Log.e(TAG, "Response from url: " + jsonStr0);
|
|
|
|
|
|
|
|
if (jsonStr0 != null) {
|
|
|
|
try {
|
|
|
|
JSONObject jsonObj = new JSONObject(jsonStr0);
|
|
|
|
|
|
|
|
// Getting JSON Array node
|
2022-10-28 20:52:08 +02:00
|
|
|
JSONArray data = jsonObj.getJSONArray("systems");
|
2018-11-25 11:54:36 +01:00
|
|
|
|
|
|
|
// looping through All data
|
|
|
|
for (int i = 0; i < data.length(); i++) {
|
|
|
|
JSONObject c = data.getJSONObject(i);
|
2022-10-28 20:52:08 +02:00
|
|
|
HashMap<String, String> service = new HashMap<>();
|
|
|
|
if (c.has("description")&&!c.isNull("description")){
|
|
|
|
String description = c.getString("description");
|
|
|
|
service.put("description", description);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
service.put("description", "No Description");
|
|
|
|
}
|
2018-11-25 11:54:36 +01:00
|
|
|
|
|
|
|
String name = c.getString("name");
|
2022-10-28 20:52:08 +02:00
|
|
|
String category = c.getString("category");
|
|
|
|
String status = c.getString("status");
|
2018-11-25 11:54:36 +01:00
|
|
|
|
|
|
|
// tmp hash map for single service
|
|
|
|
// adding each child node to HashMap key => value
|
|
|
|
service.put("name", name);
|
2022-10-28 20:52:08 +02:00
|
|
|
service.put("category", category);
|
|
|
|
service.put("status", status);
|
2018-11-25 11:54:36 +01:00
|
|
|
|
|
|
|
// adding service to service list
|
|
|
|
stateList.add(service);
|
|
|
|
}
|
|
|
|
} catch (final JSONException e) {
|
|
|
|
Log.e(TAG, "Json parsing error: " + e.getMessage());
|
2022-10-29 02:01:02 +02:00
|
|
|
runOnUiThread( () -> Toast.makeText(getApplicationContext(),
|
|
|
|
"Json parsing error: " + e.getMessage(),
|
|
|
|
Toast.LENGTH_LONG)
|
|
|
|
.show() );
|
2018-11-25 11:54:36 +01:00
|
|
|
}
|
2022-10-29 02:01:02 +02:00
|
|
|
}
|
|
|
|
else {
|
2018-11-25 11:54:36 +01:00
|
|
|
Log.e(TAG, "Couldn't get json from server.");
|
2022-10-29 02:01:02 +02:00
|
|
|
runOnUiThread( () -> Toast.makeText(getApplicationContext(),
|
|
|
|
"Couldn't get json from server. Is your internet connection ok?",
|
|
|
|
Toast.LENGTH_LONG)
|
|
|
|
.show() );
|
2018-11-25 11:54:36 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Void result) {
|
|
|
|
super.onPostExecute(result);
|
|
|
|
// Dismiss the progress dialog
|
|
|
|
if (pDialog.isShowing())
|
|
|
|
pDialog.dismiss();
|
|
|
|
|
|
|
|
//Updating parsed JSON data into ListView
|
|
|
|
ListAdapter adapter = new SimpleAdapter(
|
2022-10-29 02:01:02 +02:00
|
|
|
StateActivity.this, stateList, R.layout.list_services,
|
2022-10-28 20:52:08 +02:00
|
|
|
new String[]{"name", "description", "category","status"},
|
2022-10-29 02:01:02 +02:00
|
|
|
new int[]{R.id.name,R.id.description,R.id.category, R.id.status})
|
2018-11-25 22:39:04 +01:00
|
|
|
|
2022-10-29 02:01:02 +02:00
|
|
|
//Change Color based on Status
|
2018-11-25 22:39:04 +01:00
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public View getView(int position, View convertView, ViewGroup parent) {
|
|
|
|
View v = super.getView(position, convertView, parent);
|
2022-10-28 20:52:08 +02:00
|
|
|
|
2018-11-27 01:43:42 +01:00
|
|
|
//Status
|
2022-10-29 02:01:02 +02:00
|
|
|
TextView status = v.findViewById(R.id.status);
|
2018-11-25 22:39:04 +01:00
|
|
|
String statusValue = status.getText().toString();
|
|
|
|
switch (statusValue) {
|
2022-10-28 20:52:08 +02:00
|
|
|
case "ok":
|
2018-11-25 22:39:04 +01:00
|
|
|
status.setTextColor(Color.GREEN);
|
2018-11-27 01:43:42 +01:00
|
|
|
status.setText(R.string.Operational);
|
2018-11-25 22:39:04 +01:00
|
|
|
break;
|
2022-10-28 20:52:08 +02:00
|
|
|
case "down":
|
2018-11-25 22:39:04 +01:00
|
|
|
status.setTextColor(Color.RED);
|
2022-10-28 20:52:08 +02:00
|
|
|
status.setText(R.string.down);
|
2018-11-25 22:39:04 +01:00
|
|
|
break;
|
2022-10-28 20:52:08 +02:00
|
|
|
default:
|
|
|
|
status.setTextColor(Color.RED);
|
2018-11-25 22:39:04 +01:00
|
|
|
break;
|
2022-10-28 20:52:08 +02:00
|
|
|
|
2018-11-25 22:39:04 +01:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
};
|
2018-11-25 11:54:36 +01:00
|
|
|
lv.setAdapter(adapter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|