/*
 * Copyright 2016 Peter Kenji Yamanaka
 *
 * 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.pyamsoft.pydroid.about;

import android.content.Context;
import android.content.res.AssetManager;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import javax.inject.Inject;
import rx.Observable;
import timber.log.Timber;

class AboutLibrariesInteractorImpl implements AboutLibrariesInteractor {

  @SuppressWarnings("WeakerAccess") @NonNull final HashMap<String, String> cachedLicenses;
  @SuppressWarnings("WeakerAccess") @NonNull private final LicenseProvider licenseProvider;
  @SuppressWarnings("WeakerAccess") @NonNull private final AssetManager assetManager;

  @Inject AboutLibrariesInteractorImpl(@NonNull Context context) {
    assetManager = context.getAssets();
    licenseProvider = Licenses.licenses(context);
    cachedLicenses = new HashMap<>();
  }

  @Override public void clearCache() {
    cachedLicenses.clear();
  }

  @SuppressWarnings("WeakerAccess") @VisibleForTesting @NonNull @CheckResult
  Observable<String> loadNewLicense(@NonNull String licenseName, @NonNull String licenseLocation) {
    return Observable.defer(() -> {
      if (licenseLocation.isEmpty()) {
        Timber.w("Empty license passed");
        return Observable.just("");
      }

      if (licenseName.equals(Licenses.Names.GOOGLE_PLAY)) {
        Timber.d("License is Google Play services");
        final String googleOpenSourceLicenses = licenseProvider.provideGoogleOpenSourceLicenses();
        final Observable<String> result = Observable.just(
            googleOpenSourceLicenses == null ? "Unable to load Google Play Open Source Licenses"
                : googleOpenSourceLicenses);
        Timber.i("Finished loading Google Play services license");
        return result;
      }

      String licenseText;
      final StringBuilder text = new StringBuilder();
      try (
          final InputStream fileInputStream = assetManager.open(licenseLocation);
          final BufferedReader br = new BufferedReader(
              new InputStreamReader(fileInputStream, StandardCharsets.UTF_8))) {
        String line = br.readLine();
        while (line != null) {
          text.append(line).append('\n');
          line = br.readLine();
        }
        licenseText = text.toString();
      } catch (IOException e) {
        e.printStackTrace();
        licenseText = "Could not load license text";
      }

      return Observable.just(licenseText);
    });
  }

  @NonNull @Override public Observable<String> loadLicenseText(@NonNull AboutLicenseItem license) {
    return Observable.defer(() -> {
      if (cachedLicenses.containsKey(license.getName())) {
        Timber.d("Fetch from cache");
        return Observable.just(cachedLicenses.get(license.getName()));
      } else {
        Timber.d("Load from asset location");
        return loadNewLicense(license.getName(), license.getLicenseLocation());
      }
    }).map(licenseText -> {
      if (!cachedLicenses.containsKey(license.getName())) {
        Timber.d("Put into cache");
        cachedLicenses.put(license.getName(), licenseText);
      }

      return licenseText;
    });
  }
}

@@ -704,24 +704,49 @@
 setManager;%0A
+import android.os.Build;%0A
 import andro
@@ -2645,84 +2645,13 @@
     
-final StringBuilder text = new StringBuilder();%0A      try (%0A          final 
+try (
 Inpu
@@ -2710,17 +2710,20 @@
 ocation)
-;
+) %7B%0A
 %0A       
@@ -2727,55 +2727,139 @@
     
-  final BufferedReader br = new Buffered
+// Standard Charsets is only KitKat, add this extra check to support Home Button%0A        final InputStreamReader inputStream
 Reader
-(
+;
 %0A   
@@ -2867,81 +2867,402 @@
     
-      new InputStreamReader(fileInputStream, StandardCharsets.UTF_8))) %7B%0A
+if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.KITKAT) %7B%0A          inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);%0A        %7D else %7B%0A          inputStreamReader = new InputStreamReader(fileInputStream, %22UTF-8%22);%0A        %7D%0A%0A        try (BufferedReader br = new BufferedReader(inputStreamReader)) %7B%0A          final StringBuilder text = new StringBuilder();%0A  
     
@@ -3286,32 +3286,34 @@
  br.readLine();%0A
+  
         while (l
@@ -3323,24 +3323,26 @@
  != null) %7B%0A
+  
           te
@@ -3381,16 +3381,18 @@
         
+  
 line = b
@@ -3397,32 +3397,34 @@
  br.readLine();%0A
+  
         %7D%0A      
@@ -3417,32 +3417,34 @@
       %7D%0A        
+  
 licenseText = te
@@ -3454,24 +3454,34 @@
 toString();%0A
+        %7D%0A
       %7D catc

Fix crash on API 16 Home Button