import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.support.v4.widget.*;
import android.graphics.*;
import android.view.animation.*;
import layout.*;
public class MainActivity extends Activity {
private SwipeRefreshLayout swipeRefreshMember;
private ListView listMember;
private Button btnRegister;
private String URLserver = "https://hellopapa.000webhostapp.com/key/panel/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipeRefreshMember = findViewById(R.id.swipeRefreshMember);
listMember = findViewById(R.id.listMember);
btnRegister = findViewById(R.id.btnRegister);
swipeRefreshMember.setProgressViewOffset(true, 0, 64);
swipeRefreshMember.setColorSchemeColors(0xFF2C65FC, 0xFF2C65FC);
swipeRefreshMember.setProgressBackgroundColorSchemeColor(0xFFFFFFFF);
swipeRefreshMember.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
onResume();
}
});
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setView(R.layout.register);
dialog.setCancelable(true);
dialog.show();
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_in);
LayoutAnimationController layoutAnimation = new LayoutAnimationController(animation);
layoutAnimation.setOrder(LayoutAnimationController.ORDER_NORMAL);
layoutAnimation.setDelay(0.3);
animationLayout.setLayoutAnimation(layoutAnimation);
final String[] expType = {"1 days"};
CheckBox oneDay = findViewById(R.id.oneDay);
CheckBox oneWeek = findViewById(R.id.oneWeek);
CheckBox twoWeek = findViewById(R.id.twoWeek);
CheckBox oneMonth = findViewById(R.id.oneMonth);
oneDay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
expType[0] = "1 days";
}
}
});
oneWeek.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
expType[0] = "1 week";
}
}
});
twoWeek.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
expType[0] = "2 week";
}
}
});
oneMonth.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
expType[0] = "1 month";
}
}
});
Button btnCreate = findViewById(R.id.btnCreate);
final EditText txtUsername = findViewById(R.id.txtUsername);
final EditText txtPassword = findViewById(R.id.txtPassword);
btnCreate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = txtUsername.getText().toString();
String password = txtPassword.getText().toString();
if (username.isEmpty()) {
Toast.makeText(MainActivity.this, "Username is required.", Toast.LENGTH_SHORT).show();
} else if (password.isEmpty()) {
Toast.makeText(MainActivity.this, "Password is required.", Toast.LENGTH_SHORT).show();
} else {
createNewUser(username, password, expType[0]);
}
}
});
}
});
}
@Override
protected void onResume() {
super.onResume();
swipeRefreshMember.setRefreshing(true);
Http.get(URLserver + "list.php", null, "utf8", null, new HttpCallback() {
@Override
public void onResponse(int code, String body, String cookie, String header) {
swipeRefreshMember.setRefreshing(false);
if (code == 200) {
loadstring(body);
List<Map<String, Object>> data = new ArrayList<>();
LuaAdapter adp = new LuaAdapter(MainActivity.this, data, R.layout.memberitem);
for (int n = 1; n <= jumlah; n++) {
Map<String, Object> item = new HashMap<>();
item.put("username", username[n]);
item.put("uuid", uuid[n]);
item.put("expired", expDate[n]);
item.put("btnUpdate", new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setView(R.layout.update);
dialog.setCancelable(true);
dialog.show();
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_in);
LayoutAnimationController layoutAnimation = new LayoutAnimationController(animation);
layoutAnimation.setOrder(LayoutAnimationController.ORDER_NORMAL);
layoutAnimation.setDelay(0.3);
animationLayout.setLayoutAnimation(layoutAnimation);
final EditText txtUsername = findViewById(R.id.txtUsername);
final EditText txtPassword = findViewById(R.id.txtPassword);
final EditText txtUUID = findViewById(R.id.txtUUID);
final EditText txtExpired = findViewById(R.id.txtExpired);
txtUsername.setText(username[n]);
txtPassword.setText(password[n]);
txtUUID.setText(uuid[n]);
txtExpired.setText(expDate[n]);
final String[] expType = {""};
CheckBox oneDay = findViewById(R.id.oneDay);
CheckBox oneWeek = findViewById(R.id.oneWeek);
CheckBox twoWeek = findViewById(R.id.twoWeek);
CheckBox oneMonth = findViewById(R.id.oneMonth);
oneDay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
expType[0] = "1 days";
}
}
});
oneWeek.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
expType[0] = "1 week";
}
}
});
twoWeek.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
expType[0] = "2 week";
}
}
});
oneMonth.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
expType[0] = "1 month";
}
}
});
Button btnUpdate = findViewById(R.id.btnUpdate);
btnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = txtUsername.getText().toString();
String password = txtPassword.getText().toString();
if (username.isEmpty()) {
Toast.makeText(MainActivity.this, "Username is required.", Toast.LENGTH_SHORT).show();
} else if (password.isEmpty()) {
Toast.makeText(MainActivity.this, "Password is required.", Toast.LENGTH_SHORT).show();
} else {
updateUser(username, password, txtUUID.getText().toString(), expType[0]);
}
}
});
}
});
item.put("btnDelete", new View.OnClickListener() {
@Override
public void onClick(View v) {
Http.post(URLserver + "delete.php", "username=" + username[n], null, "utf8", null, new HttpCallback() {
@Override
public void onResponse(int code, String body, String cookie, String header) {
if (code == 200) {
if (body.contains("Delete success")) {
onResume();
Toast.makeText(MainActivity.this, "Delete success.", Toast.LENGTH_SHORT).show();
} else if (body.contains("Username not registered")) {
Toast.makeText(MainActivity.this, "Username not registered.", Toast.LENGTH_SHORT).show();
} else if (body.contains("Delete failed")) {
Toast.makeText(MainActivity.this, "Delete failed.", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, "Can't connect to server.", Toast.LENGTH_SHORT).show();
}
}
});
}
});
data.add(item);
}
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, data, R.layout.memberitem, new String[]{"username", "uuid", "expired"}, new int[]{R.id.username, R.id.uuid, R.id.expired});
listMember.setAdapter(adapter);
} else {
onResume();
}
}
});
}
private void createNewUser(String username, String password, String expType) {
ProgressDialog dl = ProgressDialog.show(MainActivity.this, null, "Please wait!");
dl.show();
Http.post(URLserver + "register.php", "username=" + username + "&password=" + password + "&expType=" + expType, null, "utf8", null, new HttpCallback() {
@Override
public void onResponse(int code, String body, String cookie, String header) {
dl.dismiss();
if (code == 200) {
if (body.contains("Register success")) {
onResume();
Toast.makeText(MainActivity.this, "Register success.", Toast.LENGTH_SHORT).show();
} else if (body.contains("Already registered")) {
Toast.makeText(MainActivity.this, "Username already registered.", Toast.LENGTH_SHORT).show();
} else if (body.contains("Register failed")) {
Toast.makeText(MainActivity.this, "Register failed.", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, "Can't connect to server.", Toast.LENGTH_SHORT).show();
}
}
});
}
private void updateUser(String username, String password, String uuid, String expType) {
ProgressDialog dl = ProgressDialog.show(MainActivity.this, null, "Please wait!");
dl.show();
Http.post(URLserver + "update.php", "username=" + username + "&password=" + password + "&uuid=" + uuid + "&expType=" + expType, null, "utf8", null, new HttpCallback() {
@Override
public void onResponse(int code, String body, String cookie, String header) {
dl.dismiss();
if (code == 200) {
if (body.contains("Update success")) {
onResume();
Toast.makeText(MainActivity.this, "Update success.", Toast.LENGTH_SHORT).show();
} else if (body.contains("Username not registered")) {
Toast.makeText(MainActivity.this, "Username not registered.", Toast.LENGTH_SHORT).show();
} else if (body.contains("Update failed")) {
Toast.makeText(MainActivity.this, "Update failed.", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, "Can't connect to server.", Toast.LENGTH_SHORT).show();
}
}
});
}
}