layoutText = new HashMap<>();
SharedPreferences layouts;
+ SharedPreferences options;
+ String lastToast;
NotificationManager iconManager;
@@ -38,11 +40,11 @@ implements SharedPreferences.OnSharedPreferenceChangeListener{
serviceInfo.notificationTimeout = 100;
this.setServiceInfo(serviceInfo);
layouts = getSharedPreferences("layouts", 0);
+ options = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
+ lastToast = "EMPT";
+ //options.registerOnSharedPreferenceChangeListener(this);
iconManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
-
- //layoutText.put("Русский", "RU");
- //layoutText.put("Буквы (АБВ)", "EN");
}
protected void updateNotification(String toast)
@@ -56,22 +58,26 @@ implements SharedPreferences.OnSharedPreferenceChangeListener{
layoutsEditor.commit();
textIcon = "??";
}
- Icon smallIcon = Icon.createWithBitmap(textAsBitmap(textIcon, 48, Color.WHITE));
+ Icon smallIcon = Icon.createWithBitmap(textAsBitmap(textIcon,
+ Integer.parseInt(options.getString("textSize", "48")),
+ options.getBoolean("textFakeBold", true),
+ Color.WHITE));
indicator = new Notification.Builder(this)
.setSmallIcon(smallIcon)
.setContentTitle(toast)
.setOngoing(true)
+ .setPriority(Integer.parseInt(options.getString("notificationImportance", "0")))
.build();
iconManager.notify(0, indicator);
return;
}
//Borrowed from Ted Hopp from StackOverflow
- public Bitmap textAsBitmap(String text, float textSize, int textColor) {
+ public Bitmap textAsBitmap(String text, float textSize, boolean fakeBold, int textColor) {
Paint paint = new Paint(ANTI_ALIAS_FLAG);
paint.setTextSize(textSize);
- paint.setFakeBoldText(true);
+ paint.setFakeBoldText(fakeBold);
paint.setColor(textColor);
paint.setTextAlign(Paint.Align.LEFT);
float baseline = -paint.ascent(); // ascent() is negative
@@ -91,10 +97,10 @@ implements SharedPreferences.OnSharedPreferenceChangeListener{
{
Log.d(TAG, "Caught a Toast: ");
Log.d(TAG, (String)event.getPackageName());
- String toast = (String)event.getText().get(0);
- Log.d(TAG, toast);
+ lastToast = (String)event.getText().get(0);
+ Log.d(TAG, lastToast);
- updateNotification(toast);
+ updateNotification(lastToast);
}
else
{
@@ -110,6 +116,6 @@ implements SharedPreferences.OnSharedPreferenceChangeListener{
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
- layouts = getSharedPreferences("layouts", 0);
+ updateNotification(lastToast);
}
}
diff --git a/app/src/main/java/space/neothefox/laytray/MainActivity.java b/app/src/main/java/space/neothefox/laytray/MainActivity.java
index 92667bd..d109407 100644
--- a/app/src/main/java/space/neothefox/laytray/MainActivity.java
+++ b/app/src/main/java/space/neothefox/laytray/MainActivity.java
@@ -1,11 +1,16 @@
package space.neothefox.laytray;
+import android.content.Context;
+import android.content.Intent;
import android.content.SharedPreferences;
-import android.support.constraint.Guideline;
+import android.provider.Settings;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
-import android.view.Gravity;
+import android.text.TextUtils;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
@@ -13,8 +18,8 @@ import android.widget.EditText;
import android.widget.LinearLayout;
import android.util.Log;
import android.widget.Space;
+import android.widget.Toast;
-import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity
@@ -38,6 +43,12 @@ implements View.OnClickListener, SharedPreferences.OnSharedPreferenceChangeListe
saveButton.setOnClickListener(this);
layouts.registerOnSharedPreferenceChangeListener(this);
+ if (!isAccessibilitySettingsOn(getApplicationContext())) {
+ Toast.makeText(this, "You have to enable the service!",
+ Toast.LENGTH_LONG).show();
+ startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
+ }
+
}
protected void updateLayouts()
@@ -55,7 +66,8 @@ implements View.OnClickListener, SharedPreferences.OnSharedPreferenceChangeListe
entry.getValue().toString());
i++;
- addLine(layoutLister, entry.getKey(), entry.getValue().toString());
+ if(entry.getKey() != "EMPT")
+ addLine(layoutLister, entry.getKey(), entry.getValue().toString());
}
if(i == 0)
{
@@ -76,6 +88,7 @@ implements View.OnClickListener, SharedPreferences.OnSharedPreferenceChangeListe
layoutsEditor.clear();
layoutsEditor.putString("Русский", "RU");
layoutsEditor.putString("Буквы (АБВ)", "EN");
+ layoutsEditor.putString("EMPT", "??");
layoutsEditor.commit();
}
@@ -153,6 +166,46 @@ implements View.OnClickListener, SharedPreferences.OnSharedPreferenceChangeListe
}
}
+
+ //Accessibility check by Antoine Bolvy
+ private boolean isAccessibilitySettingsOn(Context mContext) {
+ int accessibilityEnabled = 0;
+ final String service = getPackageName() + "/" + IconService.class.getCanonicalName();
+ try {
+ accessibilityEnabled = Settings.Secure.getInt(
+ mContext.getApplicationContext().getContentResolver(),
+ android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
+ Log.v(TAG, "accessibilityEnabled = " + accessibilityEnabled);
+ } catch (Settings.SettingNotFoundException e) {
+ Log.e(TAG, "Error finding setting, default accessibility to not found: "
+ + e.getMessage());
+ }
+ TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
+
+ if (accessibilityEnabled == 1) {
+ Log.v(TAG, "Accessibility service enabled");
+ String settingValue = Settings.Secure.getString(
+ mContext.getApplicationContext().getContentResolver(),
+ Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
+ if (settingValue != null) {
+ mStringColonSplitter.setString(settingValue);
+ while (mStringColonSplitter.hasNext()) {
+ String accessibilityService = mStringColonSplitter.next();
+
+ Log.v(TAG, "-------------- > accessibilityService :: " + accessibilityService + " " + service);
+ if (accessibilityService.equalsIgnoreCase(service)) {
+ Log.v(TAG, "We've found the correct setting - accessibility is switched on!");
+ return true;
+ }
+ }
+ }
+ } else {
+ Log.v(TAG, "Accessibility is disabled");
+ }
+
+ return false;
+ }
+
@Override
public void onClick(View v) {
switch(v.getId())
@@ -166,6 +219,28 @@ implements View.OnClickListener, SharedPreferences.OnSharedPreferenceChangeListe
}
}
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ MenuInflater inflater = getMenuInflater();
+ inflater.inflate(R.menu.menu, menu);
+ return true;
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ switch(item.getItemId())
+ {
+ case R.id.settings:
+ startActivity(new Intent(this, SettingsActivity.class));
+ return true;
+ case R.id.about:
+ startActivity(new Intent(this, AboutActivity.class));
+ return true;
+ default:
+ return super.onOptionsItemSelected(item);
+ }
+ }
+
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
layouts = getSharedPreferences("layouts", 0);
diff --git a/app/src/main/java/space/neothefox/laytray/SettingsActivity.java b/app/src/main/java/space/neothefox/laytray/SettingsActivity.java
new file mode 100644
index 0000000..3029149
--- /dev/null
+++ b/app/src/main/java/space/neothefox/laytray/SettingsActivity.java
@@ -0,0 +1,214 @@
+package space.neothefox.laytray;
+
+import android.annotation.TargetApi;
+import android.content.Context;
+import android.content.Intent;
+import android.content.res.Configuration;
+import android.media.Ringtone;
+import android.media.RingtoneManager;
+import android.net.Uri;
+import android.os.Build;
+import android.os.Bundle;
+import android.preference.ListPreference;
+import android.preference.Preference;
+import android.preference.PreferenceActivity;
+import android.support.v7.app.ActionBar;
+import android.preference.PreferenceFragment;
+import android.preference.PreferenceManager;
+import android.preference.RingtonePreference;
+import android.text.TextUtils;
+import android.view.MenuItem;
+import android.support.v4.app.NavUtils;
+
+import java.util.List;
+
+/**
+ * A {@link PreferenceActivity} that presents a set of application settings. On
+ * handset devices, settings are presented as a single list. On tablets,
+ * settings are split by category, with category headers shown to the left of
+ * the list of settings.
+ *
+ * See
+ * Android Design: Settings for design guidelines and the Settings
+ * API Guide for more information on developing a Settings UI.
+ */
+public class SettingsActivity extends AppCompatPreferenceActivity {
+
+ /**
+ * A preference value change listener that updates the preference's summary
+ * to reflect its new value.
+ */
+ private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
+ @Override
+ public boolean onPreferenceChange(Preference preference, Object value) {
+ String stringValue = value.toString();
+
+ if (preference instanceof ListPreference) {
+ // For list preferences, look up the correct display value in
+ // the preference's 'entries' list.
+ ListPreference listPreference = (ListPreference) preference;
+ int index = listPreference.findIndexOfValue(stringValue);
+
+ // Set the summary to reflect the new value.
+ preference.setSummary(
+ index >= 0
+ ? listPreference.getEntries()[index]
+ : null);
+
+ } else {
+ // For all other preferences, set the summary to the value's
+ // simple string representation.
+ preference.setSummary(stringValue);
+ }
+ return true;
+ }
+ };
+
+ /**
+ * Helper method to determine if the device has an extra-large screen. For
+ * example, 10" tablets are extra-large.
+ */
+ private static boolean isXLargeTablet(Context context) {
+ return (context.getResources().getConfiguration().screenLayout
+ & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
+ }
+
+ /**
+ * Binds a preference's summary to its value. More specifically, when the
+ * preference's value is changed, its summary (line of text below the
+ * preference title) is updated to reflect the value. The summary is also
+ * immediately updated upon calling this method. The exact display format is
+ * dependent on the type of preference.
+ *
+ * @see #sBindPreferenceSummaryToValueListener
+ */
+ private static void bindPreferenceSummaryToValue(Preference preference) {
+ // Set the listener to watch for value changes.
+ preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
+
+ // Trigger the listener immediately with the preference's
+ // current value.
+ sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
+ PreferenceManager
+ .getDefaultSharedPreferences(preference.getContext())
+ .getString(preference.getKey(), ""));
+ }
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setupActionBar();
+ }
+
+ /**
+ * Set up the {@link android.app.ActionBar}, if the API is available.
+ */
+ private void setupActionBar() {
+ ActionBar actionBar = getSupportActionBar();
+ if (actionBar != null) {
+ // Show the Up button in the action bar.
+ actionBar.setDisplayHomeAsUpEnabled(true);
+ }
+ }
+
+ @Override
+ public boolean onMenuItemSelected(int featureId, MenuItem item) {
+ int id = item.getItemId();
+ if (id == android.R.id.home) {
+ if (!super.onMenuItemSelected(featureId, item)) {
+ NavUtils.navigateUpFromSameTask(this);
+ }
+ return true;
+ }
+ return super.onMenuItemSelected(featureId, item);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean onIsMultiPane() {
+ return isXLargeTablet(this);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ @TargetApi(Build.VERSION_CODES.HONEYCOMB)
+ public void onBuildHeaders(List target) {
+ loadHeadersFromResource(R.xml.pref_headers, target);
+ }
+
+ /**
+ * This method stops fragment injection in malicious applications.
+ * Make sure to deny any unknown fragments here.
+ */
+ protected boolean isValidFragment(String fragmentName) {
+ return PreferenceFragment.class.getName().equals(fragmentName)
+ || GeneralPreferenceFragment.class.getName().equals(fragmentName)
+ || NotificationPreferenceFragment.class.getName().equals(fragmentName);
+ }
+
+ /**
+ * This fragment shows general preferences only. It is used when the
+ * activity is showing a two-pane settings UI.
+ */
+ @TargetApi(Build.VERSION_CODES.HONEYCOMB)
+ public static class GeneralPreferenceFragment extends PreferenceFragment {
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ addPreferencesFromResource(R.xml.pref_general);
+ setHasOptionsMenu(true);
+
+ // Bind the summaries of EditText/List/Dialog/Ringtone preferences
+ // to their values. When their values change, their summaries are
+ // updated to reflect the new value, per the Android Design
+ // guidelines.
+ bindPreferenceSummaryToValue(findPreference("default_app_name"));
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ int id = item.getItemId();
+ if (id == android.R.id.home) {
+ startActivity(new Intent(getActivity(), SettingsActivity.class));
+ return true;
+ }
+ return super.onOptionsItemSelected(item);
+ }
+ }
+
+ /**
+ * This fragment shows notification preferences only. It is used when the
+ * activity is showing a two-pane settings UI.
+ */
+ @TargetApi(Build.VERSION_CODES.HONEYCOMB)
+ public static class NotificationPreferenceFragment extends PreferenceFragment {
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ addPreferencesFromResource(R.xml.pref_notification);
+ setHasOptionsMenu(true);
+
+ // Bind the summaries of EditText/List/Dialog/Ringtone preferences
+ // to their values. When their values change, their summaries are
+ // updated to reflect the new value, per the Android Design
+ // guidelines.
+ bindPreferenceSummaryToValue(findPreference("notificationImportance"));
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ int id = item.getItemId();
+ if (id == android.R.id.home) {
+ startActivity(new Intent(getActivity(), SettingsActivity.class));
+ return true;
+ }
+ return super.onOptionsItemSelected(item);
+ }
+ }
+
+}
diff --git a/app/src/main/res/drawable/ic_info_black_24dp.xml b/app/src/main/res/drawable/ic_info_black_24dp.xml
new file mode 100644
index 0000000..34b8202
--- /dev/null
+++ b/app/src/main/res/drawable/ic_info_black_24dp.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/app/src/main/res/drawable/ic_notifications_black_24dp.xml b/app/src/main/res/drawable/ic_notifications_black_24dp.xml
new file mode 100644
index 0000000..e3400cf
--- /dev/null
+++ b/app/src/main/res/drawable/ic_notifications_black_24dp.xml
@@ -0,0 +1,9 @@
+
+
+
diff --git a/app/src/main/res/drawable/ic_sync_black_24dp.xml b/app/src/main/res/drawable/ic_sync_black_24dp.xml
new file mode 100644
index 0000000..5a283aa
--- /dev/null
+++ b/app/src/main/res/drawable/ic_sync_black_24dp.xml
@@ -0,0 +1,9 @@
+
+
+
\ No newline at end of file
diff --git a/app/src/main/res/layout/activity_about.xml b/app/src/main/res/layout/activity_about.xml
new file mode 100644
index 0000000..e0ef810
--- /dev/null
+++ b/app/src/main/res/layout/activity_about.xml
@@ -0,0 +1,47 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index aade80d..7dce49b 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -53,9 +53,10 @@
android:layout_width="81dp"
android:layout_height="41dp"
android:layout_marginEnd="8dp"
+ android:layout_marginTop="8dp"
android:text="Save"
app:layout_constraintEnd_toEndOf="parent"
- tools:layout_editor_absoluteY="16dp" />
+ app:layout_constraintTop_toTopOf="parent" />
+
\ No newline at end of file
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index d5c01c8..448c305 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -3,4 +3,38 @@
A simple layout icon
LayTray needs to be enabled as an accessibility app, because the only way to get layout info on Android is by monitoring the Toast notification. Right now it monitors Blackberry Keyboard exclusively.
Associate layout name with layout icon
+ Settings
+
+
+
+
+ General
+
+ App to monitor
+ com.blackberry.keyboard
+
+ Notification importance
+
+ - Default
+ - High
+ - Low
+ - Min
+
+
+ - 0
+ - 1
+ - -1
+ - -2
+
+
+
+
+
+ Notifications
+
+ Bold
+ Text size
+
+ A layout icon by NeoTheFox \n
+ Distributed on the terms of GNU GPLv3 licence
diff --git a/app/src/main/res/xml/pref_general.xml b/app/src/main/res/xml/pref_general.xml
new file mode 100644
index 0000000..e9df5a6
--- /dev/null
+++ b/app/src/main/res/xml/pref_general.xml
@@ -0,0 +1,10 @@
+
+
+
+
+
diff --git a/app/src/main/res/xml/pref_headers.xml b/app/src/main/res/xml/pref_headers.xml
new file mode 100644
index 0000000..456c422
--- /dev/null
+++ b/app/src/main/res/xml/pref_headers.xml
@@ -0,0 +1,15 @@
+
+
+
+
+
+
+
+
+
diff --git a/app/src/main/res/xml/pref_notification.xml b/app/src/main/res/xml/pref_notification.xml
new file mode 100644
index 0000000..8b48370
--- /dev/null
+++ b/app/src/main/res/xml/pref_notification.xml
@@ -0,0 +1,27 @@
+
+
+
+
+
+
+
+
+
+
+