Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Switch/Add servers without logging in/out #51

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 61 additions & 9 deletions app/src/main/java/com/zulip/android/ZulipApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ public class ZulipApp extends Application {
private Set<String> mutedTopics;
private static final String MUTED_TOPIC_KEY = "mutedTopics";

private final static String GLOBAL_SETTINGS = "HumbugActivity";
private final static String SERVER_SETTINGS = "serverSettings";
private final static String GLOBAL_SETTINGS_CURRENT = "currentRealm";
private static final String GLOBAL_SETTINGS_REALMS = "REALMS";


public int currentRealm = 0;

public HashSet serverStringSet;
private SharedPreferences globalSettings;
/**
* Handler to manage batching of unread messages
*/
Expand Down Expand Up @@ -98,15 +108,11 @@ public void onCreate() {

// This used to be from HumbugActivity.getPreferences, so we keep that
// file name.
this.settings = getSharedPreferences("HumbugActivity",
Context.MODE_PRIVATE);

max_message_id = settings.getInt("max_message_id", -1);
eventQueueId = settings.getString("eventQueueId", null);
lastEventId = settings.getInt("lastEventId", -1);
pointer = settings.getInt("pointer", -1);

globalSettings = getSharedPreferences(GLOBAL_SETTINGS, Context.MODE_PRIVATE);
currentRealm = globalSettings.getInt(GLOBAL_SETTINGS_CURRENT, currentRealm);
serverStringSet = new HashSet<String>(globalSettings.getStringSet(GLOBAL_SETTINGS_REALMS, new HashSet<String>()));

switchToRealm(currentRealm);
this.api_key = settings.getString(API_KEY, null);

if (api_key != null) {
Expand Down Expand Up @@ -223,7 +229,7 @@ public void addToMutedTopics(JSONArray jsonArray) {
}

public void setEmail(String email) {
databaseHelper = new DatabaseHelper(this, email);
databaseHelper = new DatabaseHelper(this, SERVER_SETTINGS + currentRealm);
this.you = Person.getOrUpdate(this, email, null, null);
}

Expand Down Expand Up @@ -379,4 +385,50 @@ private static void setInstance(ZulipApp instance) {
public boolean isTopicMute(int id, String subject) {
return mutedTopics.contains(id + subject);
}

public void saveServerName(String serverName) {
String username=null;
try {
username = you.getEmail();
} catch (Exception e) {
//SQL Exception can occur if name is not updated!
ZLog.logException(e);
Copy link
Contributor

@niftynei niftynei Aug 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what cases would username be null?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't be, as setEmail is being called always before this is called!

}
Editor globalEditor = this.globalSettings.edit();
serverStringSet.add((username != null) ? serverName + " - " + username : serverName);
globalEditor.putStringSet(GLOBAL_SETTINGS_REALMS, new HashSet<String>(serverStringSet));
globalEditor.apply();
}
public void createNewRealm() {
SharedPreferences.Editor editor = this.globalSettings.edit();
currentRealm = serverStringSet.size();
editor.putInt(GLOBAL_SETTINGS_CURRENT, currentRealm);
editor.apply();

databaseHelper = new DatabaseHelper(this, SERVER_SETTINGS + serverStringSet.size());

this.settings = getSharedPreferences(SERVER_SETTINGS + serverStringSet.size(), Context.MODE_PRIVATE);
max_message_id = settings.getInt("max_message_id", -1);
eventQueueId = settings.getString("eventQueueId", null);
lastEventId = settings.getInt("lastEventId", -1);
pointer = settings.getInt("pointer", -1);
}

public void switchToRealm(int position) {
databaseHelper = new DatabaseHelper(this, SERVER_SETTINGS + position);
this.settings = getSharedPreferences(SERVER_SETTINGS + position, Context.MODE_PRIVATE);
this.api_key = settings.getString("api_key", null);
max_message_id = settings.getInt("max_message_id", -1);
eventQueueId = settings.getString("eventQueueId", null);
lastEventId = settings.getInt("lastEventId", -1);
pointer = settings.getInt("pointer", -1);
if (settings.contains("email"))
this.you = Person.getOrUpdate(this, settings.getString("email", null), null, null);
currentRealm = position;
if (position != currentRealm) {
SharedPreferences.Editor edit = globalSettings.edit();
edit.putInt(GLOBAL_SETTINGS_CURRENT, position);
edit.apply();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.zulip.android.R;
import com.zulip.android.networking.AsyncDevGetEmails;
import com.zulip.android.networking.AsyncLogin;
import com.zulip.android.networking.LoginInterface;
import com.zulip.android.networking.ZulipAsyncPushTask;
import com.zulip.android.util.AuthClickListener;
import com.zulip.android.util.ZLog;

Expand All @@ -23,16 +25,19 @@
/**
* Activity where the Emails for the DevAuthBackend are displayed.
*/
public class DevAuthActivity extends Activity {
public class DevAuthActivity extends Activity implements LoginInterface {
private RecyclerView recyclerView;
private ProgressDialog connectionProgressDialog;

public static final int ADD_REALM_REQUEST = 566;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dev_auth);
String json = getIntent().getStringExtra(AsyncDevGetEmails.EMAIL_JSON);
final String realmName = getIntent().getStringExtra(AsyncDevGetEmails.REALM_NAME_JSON);
final String serverURL = getIntent().getStringExtra(AsyncDevGetEmails.SERVER_URL_JSON);
final boolean startedFromAddRealm = getIntent().getBooleanExtra(AsyncDevGetEmails.ADD_REALM_BOOLEAN_JSON, false);
recyclerView = (RecyclerView) findViewById(R.id.devAuthRecyclerView);
List<String> emails = new ArrayList<>();
int directAdminSize = 1;
Expand All @@ -58,15 +63,27 @@ protected void onCreate(Bundle savedInstanceState) {
authEmailAdapter.setOnItemClickListener(new AuthClickListener() {
@Override
public void onItemClick(String email) {
AsyncLogin asyncLogin = new AsyncLogin(DevAuthActivity.this, email, null, true);
AsyncLogin asyncLogin = new AsyncLogin(DevAuthActivity.this, email, null, realmName, startedFromAddRealm, serverURL, true);
asyncLogin.execute();
connectionProgressDialog.show();
asyncLogin.setCallback(new ZulipAsyncPushTask.AsyncTaskCompleteListener() {
@Override
public void onTaskComplete(String result, JSONObject jsonObject) {
connectionProgressDialog.dismiss();
}

@Override
public void onTaskFailure(String result) {
connectionProgressDialog.dismiss();
}
});
}
});
recyclerView.setLayoutManager(new LinearLayoutManager(DevAuthActivity.this) {
});
}

@Override
public void openHome() {
// Cancel before leaving activity to avoid leaking windows
connectionProgressDialog.dismiss();
Expand Down
60 changes: 52 additions & 8 deletions app/src/main/java/com/zulip/android/activities/LoginActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.zulip.android.activities;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
Expand All @@ -9,10 +10,15 @@
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.auth.api.Auth;
Expand All @@ -25,6 +31,7 @@
import com.zulip.android.R;
import com.zulip.android.networking.AsyncDevGetEmails;
import com.zulip.android.networking.AsyncGetBackends;
import com.zulip.android.networking.LoginInterface;
import com.zulip.android.util.AnimationHelper;
import com.zulip.android.util.ZLog;
import com.zulip.android.ZulipApp;
Expand All @@ -34,25 +41,31 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import static com.zulip.android.activities.DevAuthActivity.ADD_REALM_REQUEST;

/**
* Activity to Login through various backends on a specified server.
* Currently supported LoginAuths are Emailbackend and DevAuthBackend.
*/
public class LoginActivity extends AppCompatActivity implements View.OnClickListener,
GoogleApiClient.OnConnectionFailedListener {
GoogleApiClient.OnConnectionFailedListener, LoginInterface {
private static final String TAG = "LoginActivity";
private static final int REQUEST_CODE_RESOLVE_ERR = 9000;
private static final int REQUEST_CODE_SIGN_IN = 9001;

private boolean startedFromAddRealm = false;
private ProgressDialog connectionProgressDialog;
private GoogleApiClient mGoogleApiClient;
private EditText mServerEditText;
private EditText mUserName;
private EditText mPassword;
private EditText serverIn;

private EditText realmNameET;
private String serverURL;
private View mGoogleSignInButton;

@Override
Expand Down Expand Up @@ -100,6 +113,12 @@ public void onClick(View view) {
private void showLoginFields() {
AnimationHelper.showView(findViewById(R.id.serverInput), 201);
AnimationHelper.hideView(findViewById(R.id.serverFieldLayout), 100);
realmNameET = (EditText) findViewById(R.id.realmName);
if (getIntent().getBooleanExtra("FROM_ADDREALM", false)) {
startedFromAddRealm = true;
((TextView) findViewById(R.id.welcome_zulip)).setText(R.string.add_realm);
((Button) findViewById(R.id.zulip_login)).setText(R.string.add_realm_login);
}
}

@Override
Expand All @@ -110,6 +129,11 @@ protected void onActivityResult(int requestCode, int resultCode, Intent intent)
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(intent);
handleSignInResult(result);
break;
case ADD_REALM_REQUEST:
if (resultCode == Activity.RESULT_OK) {
setResult(Activity.RESULT_OK, intent);
finish();
}
default:
break;
}
Expand Down Expand Up @@ -156,6 +180,7 @@ private void checkForError() {
}
}


private void showBackends(String httpScheme, String serverURL) {
Uri serverUri = Uri.parse(serverURL);

Expand All @@ -169,8 +194,8 @@ private void showBackends(String httpScheme, String serverURL) {
serverIn.setText(serverUri.toString());
mServerEditText.setText(serverUri.toString());
mServerEditText.setEnabled(false);
((ZulipApp) getApplication()).setServerURL(serverUri.toString());
AsyncGetBackends asyncGetBackends = new AsyncGetBackends(ZulipApp.get());
this.serverURL = serverUri.toString();
AsyncGetBackends asyncGetBackends = new AsyncGetBackends(ZulipApp.get(), this.serverURL);
asyncGetBackends.setCallback(new AsyncTaskCompleteListener() {
@Override
public void onTaskComplete(String result, JSONObject jsonObject) {
Expand Down Expand Up @@ -242,7 +267,7 @@ private void handleSignInResult(GoogleSignInResult result) {
return;
}

final AsyncLogin loginTask = new AsyncLogin(LoginActivity.this, "google-oauth2-token", account.getIdToken(), false);
final AsyncLogin loginTask = new AsyncLogin(LoginActivity.this, "google-oauth2-token", account.getIdToken(), getRealmName(), startedFromAddRealm, serverURL, false);
loginTask.setCallback(new AsyncTaskCompleteListener() {
@Override
public void onTaskComplete(String result, JSONObject object) {
Expand Down Expand Up @@ -270,11 +295,26 @@ public void onTaskFailure(String result) {
}
}

private void openLegal() {
private String getRealmName() {
if (TextUtils.isEmpty(realmNameET.getText())) {
String server = serverURL;
URI uri = null;
try {
uri = new URI(server);
} catch (URISyntaxException e) {
return server;
}
return uri.getHost();
}
return realmNameET.getText().toString();
}

protected void openLegal() {
Intent i = new Intent(this, LegalActivity.class);
startActivityForResult(i, 0);
}

@Override
public void openHome() {
// Cancel before leaving activity to avoid leaking windows
connectionProgressDialog.dismiss();
Expand Down Expand Up @@ -341,10 +381,14 @@ public void onClick(View v) {
if (!isInputValid()) {
return;
}
if (!isInputValid()) return;
if (serverURL == null) return;
connectionProgressDialog.show();

AsyncLogin alog = new AsyncLogin(LoginActivity.this,
mUserName.getText().toString(), mPassword.getText().toString(), false);
mUserName.getText().toString(), mPassword.getText().toString(),
getRealmName(), startedFromAddRealm, serverURL, false);

// Remove the CPD when done
alog.setCallback(new AsyncTaskCompleteListener() {
@Override
Expand All @@ -366,7 +410,7 @@ public void onTaskFailure(String result) {
case R.id.local_server_button:
if (!isInputValidForDevAuth()) return;
connectionProgressDialog.show();
AsyncDevGetEmails asyncDevGetEmails = new AsyncDevGetEmails(LoginActivity.this);
AsyncDevGetEmails asyncDevGetEmails = new AsyncDevGetEmails(LoginActivity.this, serverURL, getRealmName(), startedFromAddRealm);
asyncDevGetEmails.setCallback(new AsyncTaskCompleteListener() {
@Override
public void onTaskComplete(String result, JSONObject jsonObject) {
Expand Down
Loading