Browse Source

Fix bug where AutoCompleteTextView selected text but didn't delete it when typing

Also added in window focus change callback so that we can animate UI in
correctly. Also other small changes
master
Anthony Restaino 9 years ago
parent
commit
b7f3defd19
  1. 54
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  2. 19
      app/src/main/java/acr/browser/lightning/activity/TabsManager.java
  3. 25
      app/src/main/java/acr/browser/lightning/activity/ThemableBrowserActivity.java
  4. 2
      app/src/main/java/acr/browser/lightning/app/AppComponent.java
  5. 2
      app/src/main/java/acr/browser/lightning/view/LightningView.java
  6. 62
      app/src/main/java/acr/browser/lightning/view/SearchView.java
  7. 3
      app/src/main/res/layout/search.xml

54
app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java

@ -43,6 +43,7 @@ import android.support.v7.app.ActionBar; @@ -43,6 +43,7 @@ import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.Toolbar;
import android.text.Selection;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
@ -125,6 +126,7 @@ import acr.browser.lightning.utils.Utils; @@ -125,6 +126,7 @@ import acr.browser.lightning.utils.Utils;
import acr.browser.lightning.utils.WebUtils;
import acr.browser.lightning.view.AnimatedProgressBar;
import acr.browser.lightning.view.LightningView;
import acr.browser.lightning.view.SearchView;
import butterknife.Bind;
import butterknife.ButterKnife;
@ -152,7 +154,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -152,7 +154,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
// Toolbar Views
private View mSearchBackground;
private Toolbar mToolbar;
private AutoCompleteTextView mSearch;
private SearchView mSearch;
private ImageView mArrowImage;
// Current tab view being displayed
@ -372,7 +374,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -372,7 +374,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
arrowButton.setOnClickListener(this);
// create the search EditText in the ToolBar
mSearch = (AutoCompleteTextView) customView.findViewById(R.id.search);
mSearch = (SearchView) customView.findViewById(R.id.search);
mSearchBackground = customView.findViewById(R.id.search_container);
// initialize search background color
@ -397,6 +399,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -397,6 +399,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mSearch.setOnFocusChangeListener(search);
mSearch.setOnEditorActionListener(search);
mSearch.setOnTouchListener(search);
mSearch.setOnPreFocusListener(search);
initializeSearchSuggestions(mSearch);
@ -437,12 +440,13 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -437,12 +440,13 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
System.exit(1);
}
private class SearchListenerClass implements OnKeyListener, OnEditorActionListener, OnFocusChangeListener, OnTouchListener {
private class SearchListenerClass implements OnKeyListener, OnEditorActionListener,
OnFocusChangeListener, OnTouchListener, SearchView.PreFocusListener {
@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
public boolean onKey(View searchView, int keyCode, KeyEvent keyEvent) {
switch (arg1) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mSearch.getWindowToken(), 0);
@ -480,20 +484,15 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -480,20 +484,15 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
}
@Override
public void onFocusChange(View v, final boolean hasFocus) {
public void onFocusChange(final View v, final boolean hasFocus) {
final LightningView currentView = mTabsManager.getCurrentTab();
if (!hasFocus && currentView != null) {
setIsLoading(currentView.getProgress() < 100);
updateUrl(currentView.getUrl(), true);
} else if (hasFocus && currentView != null) {
String url = currentView.getUrl();
if (UrlUtils.isSpecialUrl(url)) {
mSearch.setText("");
} else {
mSearch.setText(url);
}
// Hack to make sure the text gets selected
((AutoCompleteTextView) v).selectAll();
((SearchView) v).selectAll();
mIcon = mClearIcon;
mSearch.setCompoundDrawables(null, null, mClearIcon, null);
}
@ -522,6 +521,20 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -522,6 +521,20 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
}
return false;
}
@Override
public void onPreFocus() {
final LightningView currentView = mTabsManager.getCurrentTab();
if (currentView == null) {
return;
}
String url = currentView.getUrl();
if (UrlUtils.isSpecialUrl(url)) {
mSearch.setText("");
} else {
mSearch.setText(url);
}
}
}
private class DrawerLocker implements DrawerListener {
@ -612,8 +625,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -612,8 +625,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
// TODO layout transition causing memory leak
// mBrowserFrame.setLayoutTransition(new LayoutTransition());
mToolbarLayout.setTranslationY(0);
mBrowserFrame.setTranslationY(0);
setFullscreen(mPreferences.getHideStatusBarEnabled(), false);
initializeTabHeight();
@ -675,6 +686,13 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -675,6 +686,13 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mProxyUtils.updateProxySettings(this);
}
@Override
public void onWindowVisibleToUserAfterResume() {
super.onWindowVisibleToUserAfterResume();
mToolbarLayout.setTranslationY(0);
mBrowserFrame.setTranslationY(0);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
@ -925,7 +943,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -925,7 +943,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
// Use a delayed handler to make the transition smooth
// otherwise it will get caught up with the showTab code
// and cause a janky motion
mDrawerHandler.postDelayed(new Runnable() {
mDrawerHandler.postDelayed(new Runnable() {
@Override
public void run() {
mDrawerLayout.closeDrawers();
@ -1230,9 +1248,8 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -1230,9 +1248,8 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mSuggestionsAdapter.refreshPreferences();
mSuggestionsAdapter.refreshBookmarks();
}
mTabsManager.resumeAll();
mTabsManager.resumeAll(this);
initializePreferences();
mTabsManager.resume(this);
supportInvalidateOptionsMenu();
@ -1888,6 +1905,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -1888,6 +1905,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
@Override
public void showActionBar() {
if (mFullScreen) {
Log.d(TAG, "showActionBar");
if (mToolbarLayout == null)
return;

19
app/src/main/java/acr/browser/lightning/activity/TabsManager.java

@ -196,8 +196,11 @@ public class TabsManager { @@ -196,8 +196,11 @@ public class TabsManager {
* WebView when the app is open currently due to a
* bug in the WebView, where calling onResume doesn't
* consistently resume it.
*
* @param context the context needed to initialize
* the LightningView preferences.
*/
public void resumeAll() {
public void resumeAll(@NonNull Context context) {
LightningView current = getCurrentTab();
if (current != null) {
current.resumeTimers();
@ -205,6 +208,7 @@ public class TabsManager { @@ -205,6 +208,7 @@ public class TabsManager {
for (LightningView tab : mTabList) {
if (tab != null) {
tab.onResume();
tab.initializePreferences(context);
}
}
}
@ -274,19 +278,6 @@ public class TabsManager { @@ -274,19 +278,6 @@ public class TabsManager {
mCurrentTab = null;
}
/**
* Reinitializes the preferences for
* all the tabs in the list.
*
* @param context the context needed
* to initialize the preferences.
*/
public synchronized void resume(@NonNull final Context context) {
for (LightningView tab : mTabList) {
tab.initializePreferences(context);
}
}
/**
* Forwards network connection status to the WebViews.
*

25
app/src/main/java/acr/browser/lightning/activity/ThemableBrowserActivity.java

@ -3,8 +3,12 @@ package acr.browser.lightning.activity; @@ -3,8 +3,12 @@ package acr.browser.lightning.activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import java.util.ArrayDeque;
import java.util.Queue;
import javax.inject.Inject;
import acr.browser.lightning.R;
@ -17,6 +21,7 @@ public abstract class ThemableBrowserActivity extends AppCompatActivity { @@ -17,6 +21,7 @@ public abstract class ThemableBrowserActivity extends AppCompatActivity {
private int mTheme;
private boolean mShowTabsInDrawer;
private boolean mShouldRunOnResumeActions = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -33,9 +38,29 @@ public abstract class ThemableBrowserActivity extends AppCompatActivity { @@ -33,9 +38,29 @@ public abstract class ThemableBrowserActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus && mShouldRunOnResumeActions) {
mShouldRunOnResumeActions = false;
onWindowVisibleToUserAfterResume();
}
}
/**
* Called after the activity is resumed
* and the UI becomes visible to the user.
* Called by onWindowFocusChanged only if
* onResume has been called.
*/
public void onWindowVisibleToUserAfterResume() {
}
@Override
protected void onResume() {
super.onResume();
mShouldRunOnResumeActions = true;
int theme = mPreferences.getUseTheme();
boolean drawerTabs = mPreferences.getShowTabsInDrawer(!isTablet());
if (theme != mTheme || mShowTabsInDrawer != drawerTabs) {

2
app/src/main/java/acr/browser/lightning/app/AppComponent.java

@ -1,7 +1,5 @@ @@ -1,7 +1,5 @@
package acr.browser.lightning.app;
import android.preference.PreferenceFragment;
import javax.inject.Singleton;
import acr.browser.lightning.activity.BrowserActivity;

2
app/src/main/java/acr/browser/lightning/view/LightningView.java

@ -233,7 +233,7 @@ public class LightningView { @@ -233,7 +233,7 @@ public class LightningView {
* to get the default UserAgent for the WebView.
*/
@SuppressLint({"NewApi", "SetJavaScriptEnabled"})
public synchronized void initializePreferences(Context context) {
public synchronized void initializePreferences(@NonNull Context context) {
if (mWebView == null) {
return;
}

62
app/src/main/java/acr/browser/lightning/view/SearchView.java

@ -0,0 +1,62 @@ @@ -0,0 +1,62 @@
package acr.browser.lightning.view;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.AutoCompleteTextView;
public class SearchView extends AutoCompleteTextView {
public interface PreFocusListener {
void onPreFocus();
}
@Nullable private PreFocusListener mListener;
private boolean mIsBeingClicked;
private long mTimePressed;
public SearchView(Context context) {
super(context);
}
public SearchView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SearchView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setOnPreFocusListener(@Nullable PreFocusListener listener) {
mListener = listener;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mTimePressed = System.currentTimeMillis();
mIsBeingClicked = true;
break;
case MotionEvent.ACTION_CANCEL:
mIsBeingClicked = false;
break;
case MotionEvent.ACTION_UP:
if (mIsBeingClicked && !isLongPress()) {
if (mListener != null) {
mListener.onPreFocus();
}
}
break;
}
return super.onTouchEvent(event);
}
private boolean isLongPress() {
return (System.currentTimeMillis() - mTimePressed) >= ViewConfiguration.getLongPressTimeout();
}
}

3
app/src/main/res/layout/search.xml

@ -16,7 +16,7 @@ @@ -16,7 +16,7 @@
android:focusableInTouchMode="true"
android:gravity="center">
<AutoCompleteTextView
<acr.browser.lightning.view.SearchView
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="match_parent"
@ -29,7 +29,6 @@ @@ -29,7 +29,6 @@
android:paddingLeft="8dp"
android:paddingRight="5dp"
android:paddingTop="1dp"
android:selectAllOnFocus="true"
android:singleLine="true"
android:textColor="@color/gray_dark"
android:textColorHint="@color/hint_text"

Loading…
Cancel
Save