/*
 * 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.padlock.dagger.db;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.WorkerThread;
import com.pyamsoft.padlock.model.sql.PadLockDB;
import com.pyamsoft.padlock.model.sql.PadLockEntry;
import javax.inject.Inject;
import timber.log.Timber;

final class DBInteractorImpl implements DBInteractor {

  @NonNull private final Context appContext;

  @Inject public DBInteractorImpl(final @NonNull Context context) {
    appContext = context.getApplicationContext();
  }

  @WorkerThread @SuppressLint("NewApi") @Override
  public void createEntry(@NonNull String packageName, @Nullable String code, boolean system) {
    final PackageManager packageManager = appContext.getPackageManager();
    final PackageInfo packageInfo;
    try {
      packageInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
    } catch (PackageManager.NameNotFoundException e) {
      throw new RuntimeException("PackageManager threw exception: ", e);
    }
    final ActivityInfo[] activities = packageInfo.activities;
    if (activities != null) {
      PadLockEntry.newTransaction(appContext, () -> {
        for (final ActivityInfo info : activities) {
          final String activityName = info.name;
          if (activityName != null) {
            createEntry(packageName, activityName, code, system);
          }
        }
      });
    }
  }

  @WorkerThread @Override
  public void createEntry(@NonNull String packageName, @NonNull String activityName,
      @Nullable String code, boolean system) {
    Timber.d("CREATE: %s %s", packageName, activityName);
    PadLockEntry.insert(appContext, new PadLockEntry.Marshal().packageName(packageName)
        .activityName(activityName)
        .lockCode(code)
        .lockUntilTime(0)
        .ignoreUntilTime(0)
        .systemApplication(system)
        .asContentValues());
  }

  @WorkerThread @Override public void deleteEntry(@NonNull String packageName) {
    Timber.d("DELETE: all %s", packageName);
    PadLockEntry.deleteWithPackageName(appContext, packageName);
  }

  @WorkerThread @Override
  public void deleteEntry(@NonNull String packageName, @NonNull String activityName) {
    Timber.d("DELETE: %s %s", packageName, activityName);
    PadLockEntry.deleteWithPackageActivityName(appContext, packageName, activityName);
  }
}

@@ -999,27 +999,41 @@
 ock.
-model.sql.PadLockDB
+app.lockscreen.LockScreenActivity
 ;%0Aim
@@ -1082,16 +1082,68 @@
 kEntry;%0A
+import com.pyamsoft.pydroid.crash.CrashLogActivity;%0A
 import j
@@ -2182,32 +2182,200 @@
 vityName != null
+ && !activityName.equalsIgnoreCase(%0A              LockScreenActivity.class.getName()) && !activityName.equalsIgnoreCase(%0A              CrashLogActivity.class.getName())
 ) %7B%0A            

Exclude the lock screen and crash logging activities from being added to the DB