fix a crash on application start when no domain was saved in "domain" shared pref

This commit is contained in:
Conny Duck 2017-11-10 09:26:25 +01:00
parent dec7da9655
commit 8e59d2d524
1 changed files with 27 additions and 9 deletions

View File

@ -1,3 +1,18 @@
/* Copyright 2017 Andrew Dawson
*
* This file is part of Tusky.
*
* Tusky is free software: you can redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with Tusky. If
* not, see <http://www.gnu.org/licenses/>. */
package com.keylesspalace.tusky; package com.keylesspalace.tusky;
import android.content.Context; import android.content.Context;
@ -36,11 +51,9 @@ public final class NotificationPullJobCreator implements JobCreator {
static final String NOTIFICATIONS_JOB_TAG = "notifications_job_tag"; static final String NOTIFICATIONS_JOB_TAG = "notifications_job_tag";
static final int NOTIFY_ID = 6; // chosen by fair dice roll, guaranteed to be random static final int NOTIFY_ID = 6; // chosen by fair dice roll, guaranteed to be random
private MastodonApi mastodonApi;
private Context context; private Context context;
NotificationPullJobCreator(Context context) { NotificationPullJobCreator(Context context) {
this.mastodonApi = createMastodonApi(context);
this.context = context; this.context = context;
} }
@ -48,15 +61,20 @@ public final class NotificationPullJobCreator implements JobCreator {
@Override @Override
public Job create(@NonNull String tag) { public Job create(@NonNull String tag) {
if (tag.equals(NOTIFICATIONS_JOB_TAG)) { if (tag.equals(NOTIFICATIONS_JOB_TAG)) {
return new NotificationPullJob(mastodonApi, context); SharedPreferences preferences = context.getSharedPreferences(
context.getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
final String domain = preferences.getString("domain", null);
if(domain == null) {
return null;
} else {
return new NotificationPullJob(domain, context);
}
} }
return null; return null;
} }
private MastodonApi createMastodonApi(Context context) { private static MastodonApi createMastodonApi(String domain, Context context) {
SharedPreferences preferences = context.getSharedPreferences(
context.getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
final String domain = preferences.getString("domain", null);
OkHttpClient okHttpClient = OkHttpUtils.getCompatibleClientBuilder() OkHttpClient okHttpClient = OkHttpUtils.getCompatibleClientBuilder()
.addInterceptor(new AuthInterceptor(context)) .addInterceptor(new AuthInterceptor(context))
@ -80,8 +98,8 @@ public final class NotificationPullJobCreator implements JobCreator {
@NonNull private MastodonApi mastodonApi; @NonNull private MastodonApi mastodonApi;
private Context context; private Context context;
NotificationPullJob(@NonNull MastodonApi mastodonApi, Context context) { NotificationPullJob(String domain, Context context) {
this.mastodonApi = mastodonApi; this.mastodonApi = createMastodonApi(domain, context);
this.context = context; this.context = context;
} }