add basic login activity
This commit is contained in:
parent
7252046294
commit
c963c7195b
12 changed files with 330 additions and 952 deletions
|
|
@ -0,0 +1,116 @@
|
|||
package com.kabouzeid.gramophone.ui.activities;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
|
||||
import com.kabouzeid.appthemehelper.ThemeStore;
|
||||
import com.kabouzeid.gramophone.App;
|
||||
import com.kabouzeid.gramophone.R;
|
||||
import com.kabouzeid.gramophone.ui.activities.base.AbsBaseActivity;
|
||||
|
||||
import org.jellyfin.apiclient.interaction.AndroidCredentialProvider;
|
||||
import org.jellyfin.apiclient.interaction.ConnectionResult;
|
||||
import org.jellyfin.apiclient.interaction.GsonJsonSerializer;
|
||||
import org.jellyfin.apiclient.interaction.Response;
|
||||
import org.jellyfin.apiclient.interaction.VolleyHttpClient;
|
||||
import org.jellyfin.apiclient.interaction.connectionmanager.ConnectionManager;
|
||||
import org.jellyfin.apiclient.interaction.http.IAsyncHttpClient;
|
||||
import org.jellyfin.apiclient.logging.AndroidLogger;
|
||||
import org.jellyfin.apiclient.model.apiclient.ServerCredentials;
|
||||
import org.jellyfin.apiclient.model.logging.ILogger;
|
||||
import org.jellyfin.apiclient.model.serialization.IJsonSerializer;
|
||||
import org.jellyfin.apiclient.model.users.AuthenticationResult;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
public class LoginActivity extends AbsBaseActivity implements View.OnClickListener {
|
||||
public static final String TAG = SplashActivity.class.getSimpleName();
|
||||
|
||||
@BindView(R.id.toolbar)
|
||||
Toolbar toolbar;
|
||||
@BindView(R.id.username)
|
||||
EditText username;
|
||||
@BindView(R.id.password)
|
||||
EditText password;
|
||||
@BindView(R.id.server)
|
||||
EditText server;
|
||||
@BindView(R.id.login)
|
||||
Button login;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_login);
|
||||
setDrawUnderStatusbar();
|
||||
ButterKnife.bind(this);
|
||||
|
||||
setStatusbarColorAuto();
|
||||
setNavigationbarColorAuto();
|
||||
setTaskDescriptionColorAuto();
|
||||
|
||||
setUpViews();
|
||||
}
|
||||
|
||||
private void setUpViews() {
|
||||
setUpToolbar();
|
||||
setUpOnClickListeners();
|
||||
}
|
||||
|
||||
private void setUpToolbar() {
|
||||
toolbar.setBackgroundColor(ThemeStore.primaryColor(this));
|
||||
setSupportActionBar(toolbar);
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
}
|
||||
|
||||
private void setUpOnClickListeners() {
|
||||
login.setOnClickListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
|
||||
if (item.getItemId() == android.R.id.home) {
|
||||
onBackPressed();
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (v == login) {
|
||||
final Context context = this;
|
||||
IJsonSerializer jsonSerializer = new GsonJsonSerializer();
|
||||
ILogger logger = new AndroidLogger(TAG);
|
||||
IAsyncHttpClient httpClient = new VolleyHttpClient(logger, this);
|
||||
|
||||
AndroidCredentialProvider credentialProvider = new AndroidCredentialProvider(jsonSerializer, this, logger);
|
||||
ConnectionManager connectionManager = App.getConnectionManager(context, jsonSerializer, logger, httpClient);
|
||||
connectionManager.Connect(server.getText().toString(), new Response<ConnectionResult>() {
|
||||
@Override
|
||||
public void onResponse(ConnectionResult result) {
|
||||
App.setApiClient(result.getApiClient());
|
||||
ServerCredentials serverCredentials = new ServerCredentials();
|
||||
serverCredentials.AddOrUpdateServer(result.getServers().get(0));
|
||||
App.getApiClient().AuthenticateUserAsync(username.getText().toString(), password.getText().toString(), new Response<AuthenticationResult>() {
|
||||
@Override
|
||||
public void onResponse(AuthenticationResult result) {
|
||||
if (result.getAccessToken() == null) return;
|
||||
serverCredentials.GetServer(result.getServerId()).setAccessToken(result.getAccessToken());
|
||||
credentialProvider.SaveCredentials(serverCredentials);
|
||||
context.startActivity(new Intent(context, MainActivity.class));
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -53,8 +53,6 @@ import butterknife.ButterKnife;
|
|||
public class MainActivity extends AbsSlidingMusicPanelActivity {
|
||||
|
||||
public static final String TAG = MainActivity.class.getSimpleName();
|
||||
public static final int INTRO_REQUEST = 100;
|
||||
public static final int PURCHASE_REQUEST = 101;
|
||||
|
||||
private static final int LIBRARY = 0;
|
||||
private static final int FOLDERS = 1;
|
||||
|
|
@ -70,8 +68,6 @@ public class MainActivity extends AbsSlidingMusicPanelActivity {
|
|||
@Nullable
|
||||
private View navigationDrawerHeader;
|
||||
|
||||
private boolean blockRequestPermissions;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
|
@ -94,7 +90,6 @@ public class MainActivity extends AbsSlidingMusicPanelActivity {
|
|||
private void setMusicChooser(int key) {
|
||||
if (!App.isProVersion() && key == FOLDERS) {
|
||||
Toast.makeText(this, R.string.folder_view_is_a_pro_feature, Toast.LENGTH_LONG).show();
|
||||
startActivityForResult(new Intent(this, PurchaseActivity.class), PURCHASE_REQUEST);
|
||||
key = LIBRARY;
|
||||
}
|
||||
|
||||
|
|
@ -120,27 +115,6 @@ public class MainActivity extends AbsSlidingMusicPanelActivity {
|
|||
currentFragment = (MainActivityFragmentCallbacks) getSupportFragmentManager().findFragmentById(R.id.fragment_container);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
if (requestCode == INTRO_REQUEST) {
|
||||
blockRequestPermissions = false;
|
||||
if (!hasPermissions()) {
|
||||
requestPermissions();
|
||||
}
|
||||
checkSetUpPro(); // good chance that pro version check was delayed on first start
|
||||
} else if (requestCode == PURCHASE_REQUEST) {
|
||||
if (resultCode == RESULT_OK) {
|
||||
checkSetUpPro();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void requestPermissions() {
|
||||
if (!blockRequestPermissions) super.requestPermissions();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected View createContentView() {
|
||||
@SuppressLint("InflateParams")
|
||||
|
|
@ -166,7 +140,7 @@ public class MainActivity extends AbsSlidingMusicPanelActivity {
|
|||
new Handler().postDelayed(() -> setMusicChooser(FOLDERS), 200);
|
||||
break;
|
||||
case R.id.buy_pro:
|
||||
new Handler().postDelayed(() -> startActivityForResult(new Intent(MainActivity.this, PurchaseActivity.class), PURCHASE_REQUEST), 200);
|
||||
new Handler().postDelayed(() -> startActivity(new Intent(MainActivity.this, PurchaseActivity.class)), 200);
|
||||
break;
|
||||
case R.id.action_scan:
|
||||
new Handler().postDelayed(() -> {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,60 @@
|
|||
package com.kabouzeid.gramophone.ui.activities;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.kabouzeid.gramophone.App;
|
||||
import com.kabouzeid.gramophone.R;
|
||||
import com.kabouzeid.gramophone.ui.activities.base.AbsBaseActivity;
|
||||
|
||||
import org.jellyfin.apiclient.interaction.AndroidCredentialProvider;
|
||||
import org.jellyfin.apiclient.interaction.ConnectionResult;
|
||||
import org.jellyfin.apiclient.interaction.GsonJsonSerializer;
|
||||
import org.jellyfin.apiclient.interaction.Response;
|
||||
import org.jellyfin.apiclient.interaction.VolleyHttpClient;
|
||||
import org.jellyfin.apiclient.interaction.connectionmanager.ConnectionManager;
|
||||
import org.jellyfin.apiclient.interaction.http.IAsyncHttpClient;
|
||||
import org.jellyfin.apiclient.logging.AndroidLogger;
|
||||
import org.jellyfin.apiclient.model.apiclient.ConnectionState;
|
||||
import org.jellyfin.apiclient.model.logging.ILogger;
|
||||
import org.jellyfin.apiclient.model.serialization.IJsonSerializer;
|
||||
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
public class SplashActivity extends AbsBaseActivity {
|
||||
public static final String TAG = SplashActivity.class.getSimpleName();
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_splash);
|
||||
setDrawUnderStatusbar();
|
||||
ButterKnife.bind(this);
|
||||
|
||||
setStatusbarColorAuto();
|
||||
setNavigationbarColorAuto();
|
||||
setTaskDescriptionColorAuto();
|
||||
|
||||
IJsonSerializer jsonSerializer = new GsonJsonSerializer();
|
||||
ILogger logger = new AndroidLogger(TAG);
|
||||
IAsyncHttpClient httpClient = new VolleyHttpClient(logger, this);
|
||||
|
||||
AndroidCredentialProvider credentialProvider = new AndroidCredentialProvider(jsonSerializer, this, logger);
|
||||
if (credentialProvider.GetCredentials().getServers().size() == 0) {
|
||||
Intent intent = new Intent(this, LoginActivity.class);
|
||||
startActivity(intent);
|
||||
} else {
|
||||
final Context context = this;
|
||||
ConnectionManager connectionManager = App.getConnectionManager(this, jsonSerializer, logger, httpClient);
|
||||
connectionManager.Connect(credentialProvider.GetCredentials().getServers().get(0), new Response<ConnectionResult>() {
|
||||
@Override
|
||||
public void onResponse(ConnectionResult result) {
|
||||
if (result.getState() != ConnectionState.SignedIn) return;
|
||||
App.setApiClient(result.getApiClient());
|
||||
context.startActivity(new Intent(context, MainActivity.class));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue