/*
 * Copyright 2016 Uwe Trottmann
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.battlelancer.seriesguide.thetvdbapi;

import android.content.Context;
import com.battlelancer.seriesguide.BuildConfig;
import com.battlelancer.seriesguide.util.ServiceUtils;
import com.uwetrottmann.thetvdb.TheTvdb;
import java.io.File;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
import okhttp3.OkHttpClient;

/**
 * Extends {@link TheTvdb} to use our own caching OkHttp client.
 */
public class SgTheTvdb extends TheTvdb {

    private static final String CACHE_DIRECTORY = "thetvdb-cache";

    private static OkHttpClient cachingHttpClient;

    private final Context context;

    public SgTheTvdb(Context context) {
        super(BuildConfig.TVDB_API_KEY);
        this.context = context.getApplicationContext();
    }

    @Override
    protected synchronized OkHttpClient okHttpClient() {
        if (cachingHttpClient == null) {
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            setOkHttpClientDefaults(builder);
            builder.connectTimeout(ServiceUtils.CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
            builder.readTimeout(ServiceUtils.READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
            File cacheDir = ServiceUtils.createApiCacheDir(context, CACHE_DIRECTORY);
            builder.cache(new Cache(cacheDir, ServiceUtils.calculateApiDiskCacheSize(cacheDir)));
            cachingHttpClient = builder.build();
        }
        return cachingHttpClient;
    }
}

@@ -674,16 +674,58 @@
 ontext;%0A
+import android.content.SharedPreferences;%0A
 import c
@@ -1043,120 +1043,445 @@
 ient
-.%0A */%0Apublic class SgTheTvdb extends TheTvdb %7B%0A%0A    private static final String CACHE_DIRECTORY = %22thetvdb-cache
+ and a preferences file to store the%0A * current JSON web token long term. The token is currently valid 24 hours, so preserve it if the%0A * app gets killed to avoid an additional login endpoint call.%0A */%0Apublic class SgTheTvdb extends TheTvdb %7B%0A%0A    private static final String CACHE_DIRECTORY = %22thetvdb-cache%22;%0A    private static final String PREFERENCE_FILE = %22thetvdb-prefs%22;%0A    private static final String KEY_JSON_WEB_TOKEN = %22token
 %22;%0A%0A
@@ -1566,16 +1566,65 @@
 context;
+%0A    private final SharedPreferences preferences;
 %0A%0A    pu
@@ -1750,24 +1750,413 @@
 nContext();%0A
+        this.preferences = context.getSharedPreferences(PREFERENCE_FILE, Context.MODE_PRIVATE);%0A    %7D%0A%0A    @Override%0A    public String jsonWebToken() %7B%0A        return preferences.getString(KEY_JSON_WEB_TOKEN, null);%0A    %7D%0A%0A    @Override%0A    public void jsonWebToken(String value) %7B%0A        preferences.edit()%0A                .putString(KEY_JSON_WEB_TOKEN, value)%0A                .apply();%0A
     %7D%0A%0A    @

Store the current TVDB web token in a preferences file.