fedilab-Android-App/app/src/main/java/app/fedilab/android/activities/InstanceHealthActivity.java

171 lines
7.0 KiB
Java
Raw Normal View History

2017-11-24 18:25:25 +01:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2017-11-24 18:25:25 +01:00
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2017-11-24 18:25:25 +01:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2017-11-24 18:25:25 +01:00
* see <http://www.gnu.org/licenses>. */
2019-05-18 11:10:30 +02:00
package app.fedilab.android.activities;
2017-11-24 18:25:25 +01:00
2017-11-25 11:01:34 +01:00
import android.content.Intent;
2018-05-12 12:06:43 +02:00
import android.content.SharedPreferences;
2017-11-25 11:01:34 +01:00
import android.net.Uri;
2017-11-24 18:25:25 +01:00
import android.os.Bundle;
2017-11-25 11:01:34 +01:00
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
2017-11-24 18:25:25 +01:00
import android.view.MenuItem;
2017-11-24 19:55:06 +01:00
import android.view.View;
2017-11-25 11:01:34 +01:00
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
2017-11-24 19:55:06 +01:00
import android.widget.LinearLayout;
2017-12-05 19:36:36 +01:00
import android.widget.RelativeLayout;
2017-11-24 19:55:06 +01:00
import android.widget.TextView;
2019-11-15 16:32:25 +01:00
import androidx.core.content.ContextCompat;
2017-12-02 11:02:25 +01:00
import com.bumptech.glide.Glide;
2017-11-24 18:49:02 +01:00
import org.json.JSONObject;
2017-11-24 18:25:25 +01:00
import java.util.HashMap;
2017-11-24 19:55:06 +01:00
2019-11-15 16:32:25 +01:00
import app.fedilab.android.R;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.API;
import app.fedilab.android.client.Entities.InstanceSocial;
import app.fedilab.android.client.HttpsConnection;
import app.fedilab.android.helper.Helper;
2017-11-25 11:01:34 +01:00
2017-11-24 18:25:25 +01:00
/**
* Created by Thomas on 24/11/2017.
* Instance health activity class
*/
2017-12-12 18:17:01 +01:00
public class InstanceHealthActivity extends BaseActivity {
2017-11-24 18:25:25 +01:00
2017-11-24 18:49:02 +01:00
private InstanceSocial instanceSocial;
2017-11-24 19:55:06 +01:00
private TextView name, values, checked_at, up, uptime;
private String instance;
private LinearLayout instance_container;
private ImageView back_ground_image;
2017-12-05 19:36:36 +01:00
private RelativeLayout loader;
2017-11-24 18:25:25 +01:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2019-05-18 11:10:30 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
2018-05-12 12:06:43 +02:00
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2019-11-07 19:51:53 +01:00
if (theme == Helper.THEME_LIGHT) {
2019-11-07 19:07:19 +01:00
setTheme(R.style.Dialog);
2019-11-07 19:51:53 +01:00
} else {
2019-11-07 19:07:19 +01:00
setTheme(R.style.DialogDark);
2018-05-12 12:06:43 +02:00
}
2017-11-24 19:55:06 +01:00
setContentView(R.layout.activity_instance_social);
getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
2017-11-24 19:55:06 +01:00
Bundle b = getIntent().getExtras();
2019-09-06 17:55:14 +02:00
if (getSupportActionBar() != null)
2017-11-25 11:01:34 +01:00
getSupportActionBar().hide();
2020-04-08 12:42:15 +02:00
instance = Helper.getLiveInstance(InstanceHealthActivity.this);
2019-09-06 17:55:14 +02:00
if (b != null)
2020-04-08 12:42:15 +02:00
instance = b.getString("instance", Helper.getLiveInstance(InstanceHealthActivity.this));
2017-11-24 19:55:06 +01:00
2017-11-25 11:01:34 +01:00
Button close = findViewById(R.id.close);
2017-11-24 19:55:06 +01:00
name = findViewById(R.id.name);
values = findViewById(R.id.values);
checked_at = findViewById(R.id.checked_at);
up = findViewById(R.id.up);
uptime = findViewById(R.id.uptime);
2017-12-05 19:36:36 +01:00
instance_container = findViewById(R.id.instance_container);
loader = findViewById(R.id.loader);
back_ground_image = findViewById(R.id.back_ground_image);
2020-03-08 09:15:12 +01:00
close.setOnClickListener(view -> finish());
2017-11-25 11:01:34 +01:00
TextView ref_instance = findViewById(R.id.ref_instance);
SpannableString content = new SpannableString(ref_instance.getText().toString());
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
ref_instance.setText(content);
2020-03-08 09:15:12 +01:00
ref_instance.setOnClickListener(view -> {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://instances.social"));
startActivity(browserIntent);
2017-11-25 11:01:34 +01:00
});
2017-11-24 19:55:06 +01:00
checkInstance();
2017-11-24 18:25:25 +01:00
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
2020-03-08 09:15:12 +01:00
if (item.getItemId() == android.R.id.home) {
finish();
return true;
2017-11-24 18:25:25 +01:00
}
2020-03-08 09:15:12 +01:00
return super.onOptionsItemSelected(item);
2017-11-24 18:25:25 +01:00
}
2019-09-06 17:55:14 +02:00
private void checkInstance() {
2017-11-24 19:55:06 +01:00
2020-03-08 10:29:06 +01:00
if (instance == null) {
2019-11-23 12:13:33 +01:00
LinearLayout main_container = findViewById(R.id.main_container);
TextView no_instance = findViewById(R.id.no_instance);
instance_container.setVisibility(View.VISIBLE);
main_container.setVisibility(View.GONE);
no_instance.setVisibility(View.VISIBLE);
loader.setVisibility(View.GONE);
2017-11-24 18:25:25 +01:00
return;
2019-11-23 12:13:33 +01:00
}
2020-03-08 09:15:12 +01:00
new Thread(() -> {
try {
HashMap<String, String> parameters = new HashMap<>();
parameters.put("name", instance.trim());
final String response = new HttpsConnection(InstanceHealthActivity.this, instance).get("https://instances.social/api/1.0/instances/show", 5, parameters, Helper.THEKINRAR_SECRET_TOKEN);
if (response != null) {
2020-04-08 17:42:28 +02:00
instanceSocial = API.parseInstanceSocialResponse(new JSONObject(response));
2019-09-06 17:55:14 +02:00
}
2020-03-08 09:15:12 +01:00
runOnUiThread(() -> {
if (instanceSocial.getThumbnail() != null && !instanceSocial.getThumbnail().equals("null"))
2020-04-08 12:42:15 +02:00
Glide.with(InstanceHealthActivity.this)
2020-03-08 09:15:12 +01:00
.asBitmap()
.load(instanceSocial.getThumbnail())
.into(back_ground_image);
name.setText(instanceSocial.getName());
if (instanceSocial.isUp()) {
up.setText("Is up!");
2020-04-08 12:42:15 +02:00
up.setTextColor(ContextCompat.getColor(InstanceHealthActivity.this, R.color.green_1));
2020-03-08 09:15:12 +01:00
} else {
up.setText("Is down!");
2020-04-08 12:42:15 +02:00
up.setTextColor(ContextCompat.getColor(InstanceHealthActivity.this, R.color.red_1));
2020-03-08 09:15:12 +01:00
}
uptime.setText(String.format("Uptime: %.2f %%", (instanceSocial.getUptime() * 100)));
if (instanceSocial.getChecked_at() != null)
checked_at.setText(String.format("Checked at: %s", Helper.dateToString(instanceSocial.getChecked_at())));
values.setText(String.format("version: %s \n %s users - %s statuses", instanceSocial.getVersion(), Helper.withSuffix(instanceSocial.getUsers()), Helper.withSuffix(instanceSocial.getStatuses())));
instance_container.setVisibility(View.VISIBLE);
loader.setVisibility(View.GONE);
});
} catch (Exception e) {
runOnUiThread(() -> {
LinearLayout main_container = findViewById(R.id.main_container);
TextView no_instance = findViewById(R.id.no_instance);
instance_container.setVisibility(View.VISIBLE);
main_container.setVisibility(View.GONE);
no_instance.setVisibility(View.VISIBLE);
loader.setVisibility(View.GONE);
});
2017-11-24 18:25:25 +01:00
}
}).start();
}
}