package com.miniabs; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.text.InputType; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.TextView; public class LoginActivity extends Activity { private EditText server, user, pass; private CheckBox useKey; private TextView status, userLabel, passLabel; private Button connect; @Override protected void onCreate(Bundle b) { super.onCreate(b); setContentView(R.layout.activity_login); server = (EditText) findViewById(R.id.server); user = (EditText) findViewById(R.id.user); pass = (EditText) findViewById(R.id.pass); useKey = (CheckBox) findViewById(R.id.use_key); status = (TextView) findViewById(R.id.status); userLabel = (TextView) findViewById(R.id.user_label); passLabel = (TextView) findViewById(R.id.pass_label); connect = (Button) findViewById(R.id.connect); Prefs p = Prefs.get(this); server.setText(p.server()); user.setText(p.username()); String msg = getIntent().getStringExtra("message"); if (msg != null) status.setText(msg); useKey.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton v, boolean on) { int vis = on ? View.GONE : View.VISIBLE; user.setVisibility(vis); userLabel.setVisibility(vis); passLabel.setText(on ? "API key" : "Password"); pass.setInputType(on ? InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS : InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); pass.setText(""); } }); connect.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { go(); } }); } static String normalize(String s) { s = s.trim(); while (s.endsWith("/")) s = s.substring(0, s.length() - 1); if (s.endsWith("/login")) s = s.substring(0, s.length() - 6); if (s.length() > 0 && !s.startsWith("http://") && !s.startsWith("https://")) s = "http://" + s; return s; } private void go() { final String srv = normalize(server.getText().toString()); final String u = user.getText().toString().trim(); final String pw = pass.getText().toString(); final boolean key = useKey.isChecked(); if (srv.length() == 0) { status.setText("Enter your server address."); return; } if (key ? pw.trim().length() == 0 : u.length() == 0) { status.setText(key ? "Paste your API key." : "Enter your username."); return; } server.setText(srv); ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(pass.getWindowToken(), 0); connect.setEnabled(false); status.setText("Connecting…"); final Prefs p = Prefs.get(this); p.logout(); App.clear(); HomeActivity.clearCache(); p.setServer(srv); Trust.lastRejected = null; new Job() { String work() throws Exception { String version = Api.status(LoginActivity.this, srv); String lib; if (key) { Api.loginWithKey(LoginActivity.this, pw.trim()); lib = ""; } else { lib = Api.login(LoginActivity.this, srv, u, pw); } if (lib.length() > 0) p.setLibrary(lib, ""); return version; } void done(String version) { startActivity(new Intent(LoginActivity.this, HomeActivity.class) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)); finish(); } void failed(Exception e) { connect.setEnabled(true); status.setText(Util.describe(e)); TrustPrompt.offer(LoginActivity.this, new Runnable() { public void run() { go(); } }); } }.start(); } }