/**
 * Copyright (C) 2017 SIL International. All rights reserved.
 */

package com.tavultesoft.kmapro;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.tavultesoft.kmapro.R;
import com.tavultesoft.kmea.KMManager;

import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.support.v7.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

public class GetStartedActivity extends AppCompatActivity {

  private static ListView listView = null;
  private static ArrayList<HashMap<String, String>> list = null;
  private static KMListAdapter listAdapter = null;
  private final String iconKey = "icon";
  private final String textKey = "text";
  private final String isEnabledKey = "isEnabled";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    final Context context = this;
    setContentView(R.layout.get_started_list_layout);

    listView = (ListView) findViewById(R.id.listView);

    final ImageButton closeButton = (ImageButton) findViewById(R.id.close_button);
    closeButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        finish();
      }
    });

    final SharedPreferences prefs = getSharedPreferences(getString(R.string.kma_prefs_name), Context.MODE_PRIVATE);
    boolean dontShowGetStarted = prefs.getBoolean(MainActivity.dontShowGetStartedKey, false);

    final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
    checkBox.setChecked(dontShowGetStarted);
    checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean(MainActivity.dontShowGetStartedKey, isChecked);
        editor.commit();
      }
    });

    list = new ArrayList<HashMap<String, String>>();

    HashMap<String, String> hashMap = new HashMap<String, String>();
    hashMap.put(iconKey, "0");
    hashMap.put(textKey, getString(R.string.add_a_keyboard));
    hashMap.put(isEnabledKey, "true");
    list.add(hashMap);

    hashMap = new HashMap<String, String>();
    hashMap.put(iconKey, "0");
    hashMap.put(textKey, getString(R.string.enable_system_keyboard));
    hashMap.put(isEnabledKey, "true");
    list.add(hashMap);

    hashMap = new HashMap<String, String>();
    hashMap.put(iconKey, "0");
    hashMap.put(textKey, getString(R.string.set_keyman_as_default));
    hashMap.put(isEnabledKey, "false");
    list.add(hashMap);

    hashMap = new HashMap<String, String>();
    hashMap.put(iconKey, "0");
    hashMap.put(textKey, getString(R.string.more_info));
    hashMap.put(isEnabledKey, "true");
    list.add(hashMap);


    String[] from = new String[]{iconKey, textKey};
    int[] to = new int[]{R.id.left_icon, R.id.text};
    listAdapter = new KMListAdapter(context, list, R.layout.get_started_row_layout, from, to);
    listView.setAdapter(listAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position == 0) {
          KMManager.showLanguageList(context);
        } else if (position == 1) {
          startActivity(new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS));
        } else if (position == 2) {
          InputMethodManager imManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
          imManager.showInputMethodPicker();
        } else if (position == 3) {
          Intent i = new Intent(context, InfoActivity.class);
          i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
          startActivity(i);
          overridePendingTransition(android.R.anim.fade_in, R.anim.hold);
        }
      }
    });
  }

  @Override
  protected void onResume() {
    super.onResume();
  }

  @Override
  protected void onPause() {
    super.onPause();
  }

  @Override
  public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
      // Enumerated steps are replaced with checkboxes when the user completes a step
      String one = String.valueOf(R.drawable.ic_looks_one);
      String two = String.valueOf(R.drawable.ic_looks_two);
      String three = String.valueOf(R.drawable.ic_looks_three);
      String checkbox_on = String.valueOf(android.R.drawable.checkbox_on_background);
      String info = String.valueOf(R.drawable.ic_info_outline);

      ArrayList<HashMap<String, String>> kbList = KMManager.getKeyboardsList(this);
      if (kbList != null && kbList.size() > 1) {
        list.get(0).put(iconKey, checkbox_on);
      } else {
        list.get(0).put(iconKey, one);
      }

      if (isEnabledAsSystemKB(this)) {
        list.get(1).put(iconKey, checkbox_on);
        list.get(2).put(isEnabledKey, "true");
      } else {
        list.get(1).put(iconKey, two);
        list.get(2).put(isEnabledKey, "false");
      }

      if (isDefaultKB(this)) {
        list.get(2).put(iconKey, checkbox_on);
      } else {
        list.get(2).put(iconKey, three);
      }

      list.get(3).put(iconKey, info);

      String[] from = new String[]{iconKey, textKey};
      int[] to = new int[]{R.id.left_icon, R.id.text};
      listAdapter = new KMListAdapter(this, list, R.layout.get_started_row_layout, from, to);
      listView.setAdapter(listAdapter);
    }
  }

  protected static boolean isEnabledAsSystemKB(Context context) {
    InputMethodManager imManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    List<InputMethodInfo> imList = imManager.getEnabledInputMethodList();
    boolean isEnabled = false;
    int size = imList.size();
    for (int i = 0; i < size; i++) {
      if (imList.get(i).getServiceName().equals("com.keyman.android.SystemKeyboard")) {
        isEnabled = true;
        break;
      }
    }

    return isEnabled;
  }

  protected static boolean isDefaultKB(Context context) {
    String inputMethod = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
    return inputMethod.equals("com.tavultesoft.kmapro/com.keyman.android.SystemKeyboard");
  }
}
@@ -6790,31 +6790,36 @@
 als(
-%22
 co
-m.tavultesoft.kmapro
+ntext.getPackageName() + %22
 /com

Fix "Get Started" status for default keyboard