implement asynchronous login to prevent several common crashes
This commit is contained in:
parent
b814311804
commit
9f40b7281c
13 changed files with 235 additions and 53 deletions
|
|
@ -0,0 +1,73 @@
|
|||
package com.dkanada.gramophone.service;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.IBinder;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.dkanada.gramophone.App;
|
||||
import com.dkanada.gramophone.BuildConfig;
|
||||
import com.dkanada.gramophone.R;
|
||||
import com.dkanada.gramophone.model.User;
|
||||
import com.dkanada.gramophone.util.PreferenceUtil;
|
||||
|
||||
import org.jellyfin.apiclient.interaction.EmptyResponse;
|
||||
import org.jellyfin.apiclient.interaction.Response;
|
||||
import org.jellyfin.apiclient.model.session.ClientCapabilities;
|
||||
import org.jellyfin.apiclient.model.system.SystemInfo;
|
||||
|
||||
public class LoginService extends Service {
|
||||
public static final String PACKAGE_NAME = BuildConfig.APPLICATION_ID;
|
||||
|
||||
public static final String STATE_POLLING = PACKAGE_NAME + ".unknown";
|
||||
public static final String STATE_ONLINE = PACKAGE_NAME + ".online";
|
||||
public static final String STATE_OFFLINE = PACKAGE_NAME + ".offline";
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
sendBroadcast(new Intent(STATE_POLLING));
|
||||
authenticate();
|
||||
|
||||
return super.onStartCommand(intent, flags, startId);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private void authenticate() {
|
||||
User user = App.getDatabase().userDao().getUser(PreferenceUtil.getInstance(this).getUser());
|
||||
Context context = this;
|
||||
|
||||
if (user == null) {
|
||||
Toast.makeText(this, context.getResources().getString(R.string.error_unexpected), Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
App.getApiClient().ChangeServerLocation(user.server);
|
||||
App.getApiClient().SetAuthenticationInfo(user.token, user.id);
|
||||
App.getApiClient().GetSystemInfoAsync(new Response<SystemInfo>() {
|
||||
@Override
|
||||
public void onResponse(SystemInfo result) {
|
||||
ClientCapabilities clientCapabilities = new ClientCapabilities();
|
||||
clientCapabilities.setSupportsMediaControl(true);
|
||||
clientCapabilities.setSupportsPersistentIdentifier(true);
|
||||
|
||||
App.getApiClient().ensureWebSocket();
|
||||
App.getApiClient().ReportCapabilities(clientCapabilities, new EmptyResponse());
|
||||
|
||||
sendBroadcast(new Intent(STATE_ONLINE));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Exception exception) {
|
||||
sendBroadcast(new Intent(STATE_OFFLINE));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package com.dkanada.gramophone.service.receivers;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
|
||||
import com.dkanada.gramophone.service.LoginService;
|
||||
|
||||
public class NetworkReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo netInfo = cm.getActiveNetworkInfo();
|
||||
|
||||
// network info will be null in airplane mode
|
||||
if (netInfo != null && netInfo.isConnected()) {
|
||||
context.sendBroadcast(new Intent(LoginService.STATE_ONLINE));
|
||||
} else {
|
||||
context.sendBroadcast(new Intent(LoginService.STATE_OFFLINE));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue