/* Copyright 2017 Braden Farmer
 *
 * 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.farmerbb.secondscreen.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.inputmethodservice.InputMethodService;
import android.support.v7.app.NotificationCompat;
import android.text.InputType;
import android.view.inputmethod.EditorInfo;

import com.farmerbb.secondscreen.R;
import com.farmerbb.secondscreen.receiver.KeyboardChangeReceiver;

import java.util.Random;

public class DisableKeyboardService extends InputMethodService {

    Integer notificationId;

    @Override
    public boolean onShowInputRequested(int flags, boolean configChange) {
        return false;
    }

    @Override
    public void onStartInput(EditorInfo attribute, boolean restarting) {
        boolean isEditingText = attribute.inputType != InputType.TYPE_NULL;
        boolean hasHardwareKeyboard = getResources().getConfiguration().keyboard != Configuration.KEYBOARD_NOKEYS;

        if(notificationId == null && isEditingText && !hasHardwareKeyboard) {
            Intent keyboardChangeIntent = new Intent(this, KeyboardChangeReceiver.class);
            PendingIntent keyboardChangePendingIntent = PendingIntent.getBroadcast(this, 0, keyboardChangeIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            Notification notification = new NotificationCompat.Builder(this)
                    .setContentIntent(keyboardChangePendingIntent)
                    .setSmallIcon(android.R.drawable.stat_sys_warning)
                    .setContentTitle(getString(R.string.disabling_soft_keyboard))
                    .setContentText(getString(R.string.tap_to_change_keyboards))
                    .setPriority(Notification.PRIORITY_MIN)
                    .setOngoing(true)
                    .setShowWhen(false)
                    .build();

            notificationId = new Random().nextInt();

            NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            nm.notify(notificationId, notification);
        } else if(notificationId != null && !isEditingText) {
            NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            nm.cancel(notificationId);

            notificationId = null;
        }
    }

    @Override
    public void onDestroy() {
        if(notificationId != null) {
            NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            nm.cancel(notificationId);

            notificationId = null;
        }

        super.onDestroy();
    }
}

@@ -794,32 +794,74 @@
 content.Intent;%0A
+import android.content.SharedPreferences;%0A
 import android.c
@@ -964,17 +964,17 @@
 upport.v
-7
+4
 .app.Not
@@ -1167,16 +1167,57 @@
 eceiver;
+%0Aimport com.farmerbb.secondscreen.util.U;
 %0A%0Aimport
@@ -2071,16 +2071,30 @@
 fication
+Compat.Builder
  notific
@@ -2467,121 +2467,241 @@
 .set
-Priority(Notification.PRIORITY_MIN)%0A                    .setOngoing(true)%0A                    .setShowWhen(
+Ongoing(true)%0A                    .setShowWhen(false);%0A%0A            // Respect setting to hide notification%0A            SharedPreferences prefMain = U.getPrefMain(this);%0A            if(prefMain.getBoolean(%22hide_notification%22, 
 false)
+)
 %0A   
@@ -2717,19 +2717,58 @@
     
-    .build(
+notification.setPriority(Notification.PRIORITY_MIN
 );%0A%0A
@@ -2978,16 +2978,24 @@
 fication
+.build()
 );%0A     

Make keyboard notification respect user's notification preference