/*
 * BRLTTY - A background process providing access to the console screen (when in
 *          text mode) for a blind person using a refreshable braille display.
 *
 * Copyright (C) 1995-2018 by The BRLTTY Developers.
 *
 * BRLTTY comes with ABSOLUTELY NO WARRANTY.
 *
 * This is free software, placed under the terms of the
 * GNU Lesser General Public License, as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your option) any
 * later version. Please see the file LICENSE-LGPL for details.
 *
 * Web Page: http://brltty.com/
 *
 * This software is maintained by Dave Mielke <dave@mielke.cc>.
 */

package org.a11y.brltty.android;

import org.a11y.brltty.core.*;

import android.util.Log;

import android.accessibilityservice.AccessibilityService;
import android.view.accessibility.AccessibilityEvent;

import android.content.Intent;

public class BrailleService extends AccessibilityService {
  private static final String LOG_TAG = BrailleService.class.getName();

  private static volatile BrailleService brailleService = null;
  private Thread coreThread = null;

  public static BrailleService getBrailleService () {
    return brailleService;
  }

  public boolean launchSettingsActivity () {
    Intent intent = new Intent(this, SettingsActivity.class);

    intent.addFlags(
      Intent.FLAG_ACTIVITY_NEW_TASK |
      Intent.FLAG_ACTIVITY_CLEAR_TOP |
      Intent.FLAG_ACTIVITY_SINGLE_TOP |
      Intent.FLAG_FROM_BACKGROUND
    );

    startActivity(intent);
    return true;
  }

  @Override
  public void onCreate () {
    super.onCreate();
    Log.d(LOG_TAG, "braille service started");
    brailleService = this;
  }

  @Override
  public void onDestroy () {
    brailleService = null;
    super.onDestroy();
    Log.d(LOG_TAG, "braille service stopped");
  }

  @Override
  protected void onServiceConnected () {
    Log.d(LOG_TAG, "braille service connected");

    coreThread = new CoreThread();
    coreThread.start();
  }

  @Override
  public boolean onUnbind (Intent intent) {
    Log.d(LOG_TAG, "braille service disconnected");
    CoreWrapper.stop();

    try {
      Log.d(LOG_TAG, "waiting for core to finish");
      coreThread.join();
      Log.d(LOG_TAG, "core finished");
    } catch (InterruptedException exception) {
      Log.d(LOG_TAG, "core join failed", exception);
    }

    return false;
  }

  @Override
  public void onAccessibilityEvent (AccessibilityEvent event) {
    ScreenDriver.onAccessibilityEvent(event);
  }

  @Override
  public void onInterrupt () {
  }
}

@@ -871,16 +871,83 @@
 .Intent;
+%0Aimport android.app.PendingIntent;%0Aimport android.app.Notification;
 %0A%0Apublic
@@ -1265,45 +1265,369 @@
 %0A%0A  
-public boolean launchSettingsActivity
+@Override%0A  public void onCreate () %7B%0A    super.onCreate();%0A    Log.d(LOG_TAG, %22braille service started%22);%0A    brailleService = this;%0A  %7D%0A%0A  @Override%0A  public void onDestroy () %7B%0A    try %7B%0A      brailleService = null;%0A      Log.d(LOG_TAG, %22braille service stopped%22);%0A    %7D finally %7B%0A      super.onDestroy();%0A    %7D%0A  %7D%0A%0A  private final Intent makeSettingsIntent
  () 
@@ -1690,17 +1690,16 @@
 class);%0A
-%0A
     inte
@@ -1710,23 +1710,16 @@
 ddFlags(
-%0A      
 Intent.F
@@ -1743,159 +1743,304 @@
 TASK
- %7C%0A      Intent.FLAG_ACTIVITY_CLEAR_TOP %7C%0A      Intent.FLAG_ACTIVITY_SINGLE_TOP %7C%0A      Intent.FLAG_FROM_BACKGROUND%0A    );%0A%0A    startActivity(intent
+);%0A    return intent;%0A  %7D%0A%0A  private final Notification makeForegroundNotification () %7B%0A    Intent settingsIntent = makeSettingsIntent();%0A    settingsIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);%0A    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, makeSettingsIntent(), 0
 );%0A
+%0A
     
@@ -2050,295 +2050,295 @@
 urn 
-true;%0A  %7D%0A%0A  @Override%0A  public void onCreate () %7B%0A    super.onCreate();%0A    Log.d(LOG_TAG, %22braille service started
+new Notification.Builder(this)%0A              .setSmallIcon(R.drawable.ic_launcher)%0A              .setContentTitle(%22BRLTTY
 %22)
-;
 %0A    
-brailleService = this;%0A  %7D%0A%0A  @Override%0A  public void onDestroy () %7B%0A    brailleService = null;%0A    super.onDestroy();%0A    Log.d(LOG_TAG, %22braille service stopped%22
+          .setContentText(%22Running%22)%0A              .setContentIntent(contentIntent)%0A              .setPriority(Notification.PRIORITY_LOW)%0A              .build(
 );%0A 
@@ -2442,16 +2442,70 @@
 ected%22);
+%0A    startForeground(1, makeForegroundNotification());
 %0A%0A    co
@@ -3124,11 +3124,290 @@
 () %7B%0A  %7D
+%0A%0A  public boolean launchSettingsActivity () %7B%0A    Intent intent = makeSettingsIntent();%0A%0A    intent.addFlags(%0A      Intent.FLAG_ACTIVITY_CLEAR_TOP %7C%0A      Intent.FLAG_ACTIVITY_SINGLE_TOP %7C%0A      Intent.FLAG_FROM_BACKGROUND%0A    );%0A%0A    startActivity(intent);%0A    return true;%0A  %7D
 %0A%7D%0A

Add a foreground Android notification. (dm)