Emoji: minor bug fixes, code cleanup, logging, visibility,

This commit is contained in:
akwizgran
2016-09-23 16:48:07 +01:00
parent d5beca5351
commit c917110e6a
18 changed files with 205 additions and 205 deletions

View File

@@ -1,33 +1,14 @@
/**
* Copyright (C) 2014 Open Whisper Systems
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.thoughtcrime.securesms.components;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Rect;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
@@ -37,15 +18,23 @@ import org.briarproject.R;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;
import static android.content.Context.WINDOW_SERVICE;
import static android.view.Surface.ROTATION_270;
import static android.view.Surface.ROTATION_90;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
/**
* RelativeLayout that, when a view container, will report back when it thinks a soft keyboard
* has been opened and what its height would be.
* RelativeLayout that, when a view container, will report back when it thinks
* a soft keyboard has been opened and what its height would be.
*/
@UiThread
public class KeyboardAwareRelativeLayout extends RelativeLayout {
private static final String TAG =
KeyboardAwareRelativeLayout.class.getSimpleName();
private static final Logger LOG =
Logger.getLogger(KeyboardAwareRelativeLayout.class.getName());
private final Rect rect = new Rect();
private final Set<OnKeyboardHiddenListener> hiddenListeners =
@@ -100,7 +89,7 @@ public class KeyboardAwareRelativeLayout extends RelativeLayout {
int oldRotation = rotation;
rotation = getDeviceRotation();
if (oldRotation != rotation) {
Log.w(TAG, "rotation changed");
LOG.info("Rotation changed");
onKeyboardClose();
}
}
@@ -111,13 +100,13 @@ public class KeyboardAwareRelativeLayout extends RelativeLayout {
return;
}
if (viewInset == 0 && Build.VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP)
if (viewInset == 0 && Build.VERSION.SDK_INT >= 21)
viewInset = getViewInset();
final int availableHeight =
this.getRootView().getHeight() - statusBarHeight - viewInset;
int availableHeight =
getRootView().getHeight() - statusBarHeight - viewInset;
getWindowVisibleDisplayFrame(rect);
final int keyboardHeight = availableHeight - (rect.bottom - rect.top);
int keyboardHeight = availableHeight - (rect.bottom - rect.top);
if (keyboardHeight > minKeyboardSize) {
if (getKeyboardHeight() != keyboardHeight)
@@ -128,7 +117,7 @@ public class KeyboardAwareRelativeLayout extends RelativeLayout {
}
}
@TargetApi(VERSION_CODES.LOLLIPOP)
@TargetApi(21)
private int getViewInset() {
try {
Field attachInfoField = View.class.getDeclaredField("mAttachInfo");
@@ -141,25 +130,26 @@ public class KeyboardAwareRelativeLayout extends RelativeLayout {
Rect insets = (Rect) stableInsetsField.get(attachInfo);
return insets.bottom;
}
} catch (NoSuchFieldException nsfe) {
Log.w(TAG, "field reflection error when measuring view inset",
nsfe);
} catch (IllegalAccessException iae) {
Log.w(TAG, "access reflection error when measuring view inset",
iae);
} catch (NoSuchFieldException e) {
LOG.log(WARNING,
"field reflection error when measuring view inset", e);
} catch (IllegalAccessException e) {
LOG.log(WARNING,
"access reflection error when measuring view inset", e);
}
return 0;
}
protected void onKeyboardOpen(int keyboardHeight) {
Log.w(TAG, "onKeyboardOpen(" + keyboardHeight + ")");
if (LOG.isLoggable(INFO))
LOG.info("onKeyboardOpen(" + keyboardHeight + ")");
keyboardOpen = true;
notifyShownListeners();
}
protected void onKeyboardClose() {
Log.w(TAG, "onKeyboardClose()");
LOG.info("onKeyboardClose()");
keyboardOpen = false;
notifyHiddenListeners();
}
@@ -175,13 +165,12 @@ public class KeyboardAwareRelativeLayout extends RelativeLayout {
public boolean isLandscape() {
int rotation = getDeviceRotation();
return rotation == Surface.ROTATION_90 ||
rotation == Surface.ROTATION_270;
return rotation == ROTATION_90 || rotation == ROTATION_270;
}
private int getDeviceRotation() {
WindowManager windowManager = (WindowManager) getContext()
.getSystemService(Activity.WINDOW_SERVICE);
WindowManager windowManager =
(WindowManager) getContext().getSystemService(WINDOW_SERVICE);
return windowManager.getDefaultDisplay().getRotation();
}
@@ -190,10 +179,10 @@ public class KeyboardAwareRelativeLayout extends RelativeLayout {
}
private int getKeyboardPortraitHeight() {
int keyboardHeight =
PreferenceManager.getDefaultSharedPreferences(getContext())
.getInt("keyboard_height_portrait",
defaultCustomKeyboardSize);
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(getContext());
int keyboardHeight = prefs.getInt("keyboard_height_portrait",
defaultCustomKeyboardSize);
return clamp(keyboardHeight, minCustomKeyboardSize,
getRootView().getHeight() - minCustomKeyboardTopMargin);
}
@@ -203,8 +192,9 @@ public class KeyboardAwareRelativeLayout extends RelativeLayout {
}
private void setKeyboardPortraitHeight(int height) {
PreferenceManager.getDefaultSharedPreferences(getContext())
.edit().putInt("keyboard_height_portrait", height).apply();
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(getContext());
prefs.edit().putInt("keyboard_height_portrait", height).apply();
}
public void postOnKeyboardClose(final Runnable runnable) {
@@ -254,7 +244,8 @@ public class KeyboardAwareRelativeLayout extends RelativeLayout {
}
private void notifyHiddenListeners() {
final Set<OnKeyboardHiddenListener> listeners =
// Make a copy as listeners may remove themselves when called
Set<OnKeyboardHiddenListener> listeners =
new HashSet<>(hiddenListeners);
for (OnKeyboardHiddenListener listener : listeners) {
listener.onKeyboardHidden();
@@ -262,8 +253,8 @@ public class KeyboardAwareRelativeLayout extends RelativeLayout {
}
private void notifyShownListeners() {
final Set<OnKeyboardShownListener> listeners =
new HashSet<>(shownListeners);
// Make a copy as listeners may remove themselves when called
Set<OnKeyboardShownListener> listeners = new HashSet<>(shownListeners);
for (OnKeyboardShownListener listener : listeners) {
listener.onKeyboardShown();
}