From 85d92db738f207761f3587ca53f0a5b361b16647 Mon Sep 17 00:00:00 2001 From: Anthony Restaino Date: Sun, 23 Aug 2015 19:21:22 -0400 Subject: [PATCH] Switched to RecyclerView, cleaned up some HTML generator methods --- app/app.iml | 4 +- app/build.gradle | 2 +- .../lightning/activity/BrowserActivity.java | 262 +++++++++--------- .../lightning/constant/BookmarkPage.java | 56 ++++ .../browser/lightning/constant/StartPage.java | 110 ++++++++ .../controller/BrowserController.java | 2 +- .../browser/lightning/utils/ThemeUtils.java | 25 +- .../browser/lightning/view/LightningView.java | 114 +------- app/src/main/res/layout/tab_drawer.xml | 6 +- app/src/main/res/layout/tab_list_item.xml | 88 +++--- .../res/layout/tab_list_item_horizontal.xml | 1 + app/src/main/res/layout/toolbar.xml | 7 +- 12 files changed, 375 insertions(+), 302 deletions(-) diff --git a/app/app.iml b/app/app.iml index 718026c..1b5348c 100644 --- a/app/app.iml +++ b/app/app.iml @@ -95,9 +95,9 @@ + - @@ -117,10 +117,10 @@ - + diff --git a/app/build.gradle b/app/build.gradle index 3f0af54..a0c6770 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -46,8 +46,8 @@ dependencies { compile 'com.android.support:palette-v7:23.0.0' compile 'com.android.support:appcompat-v7:23.0.0' compile 'com.android.support:design:23.0.0' + compile 'com.android.support:recyclerview-v7:23.0.0' compile 'org.jsoup:jsoup:1.8.1' - compile 'org.lucasr.twowayview:twowayview:0.1.4' // Only Lightning Plus needs the proxy libraries lightningPlusCompile 'net.i2p.android:client:0.7' lightningPlusCompile(project(':libnetcipher')) diff --git a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java index 1df20f2..96096f0 100644 --- a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java +++ b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java @@ -20,6 +20,7 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; +import android.graphics.ColorFilter; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.graphics.Paint; @@ -45,6 +46,8 @@ import android.support.v4.widget.DrawerLayout.DrawerListener; import android.support.v7.app.ActionBar; import android.support.v7.app.AlertDialog; import android.support.v7.graphics.Palette; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.KeyEvent; @@ -89,8 +92,6 @@ import android.widget.TextView; import android.widget.TextView.OnEditorActionListener; import android.widget.VideoView; -import org.lucasr.twowayview.TwoWayView; - import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; @@ -134,7 +135,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements private FrameLayout mBrowserFrame; private FullscreenHolder mFullscreenContainer; private ListView mDrawerListRight; - private TwoWayView mDrawerListLeft; + private RecyclerView mDrawerListLeft; private LinearLayout mDrawerLeft, mDrawerRight, mUiLayout, mToolbarLayout; private RelativeLayout mSearchBar; @@ -153,7 +154,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements // Adapter private BookmarkViewAdapter mBookmarkAdapter; - private LightningViewAdapter mTitleAdapter; + private LightningViewAdapter mTabAdapter; private SearchAdapter mSearchAdapter; // Callback @@ -261,23 +262,30 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements mHomepage = mPreferences.getHomepage(); - TwoWayView horizontalListView = (TwoWayView) findViewById(R.id.twv_list); + RecyclerView horizontalListView = (RecyclerView) findViewById(R.id.twv_list); + if (mShowTabsInDrawer) { - mTitleAdapter = new LightningViewAdapter(this, R.layout.tab_list_item, mWebViewList); - mDrawerListLeft = (TwoWayView) findViewById(R.id.left_drawer_list); + mTabAdapter = new LightningViewAdapter(this, R.layout.tab_list_item, mWebViewList); + mDrawerListLeft = (RecyclerView) findViewById(R.id.left_drawer_list); mDrawerListLeft.setOverScrollMode(View.OVER_SCROLL_IF_CONTENT_SCROLLS); + RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); + mDrawerListLeft.setLayoutManager(layoutManager); + mDrawerListLeft.setHasFixedSize(true); mToolbarLayout.removeView(horizontalListView); } else { - mTitleAdapter = new LightningViewAdapter(this, R.layout.tab_list_item_horizontal, mWebViewList); + mTabAdapter = new LightningViewAdapter(this, R.layout.tab_list_item_horizontal, mWebViewList); mDrawerListLeft = horizontalListView; mDrawerListLeft.setOverScrollMode(View.OVER_SCROLL_NEVER); mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, mDrawerLeft); + RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false); + mDrawerListLeft.setLayoutManager(layoutManager); + mDrawerListLeft.setHasFixedSize(true); } - mDrawerListLeft.setAdapter(mTitleAdapter); - mDrawerListLeft.setOnItemClickListener(new DrawerItemClickListener()); - mDrawerListLeft.setOnItemLongClickListener(new DrawerItemLongClickListener()); + mDrawerListLeft.setAdapter(mTabAdapter); +// mDrawerListLeft.setOnItemClickListener(new DrawerItemClickListener()); +// mDrawerListLeft.setOnItemLongClickListener(new DrawerItemLongClickListener()); mDrawerListRight.setOnItemClickListener(new BookmarkItemClickListener()); mDrawerListRight.setOnItemLongClickListener(new BookmarkItemLongClickListener()); @@ -957,10 +965,11 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements /** * The click listener for ListView in the navigation drawer */ - private class DrawerItemClickListener implements ListView.OnItemClickListener { + private class DrawerItemClickListener implements OnClickListener { @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { + public void onClick(View v) { + int position = mDrawerListLeft.getChildAdapterPosition(v); if (mCurrentView != mWebViewList.get(position)) { mIsNewIntent = false; showTab(mWebViewList.get(position)); @@ -971,10 +980,11 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements /** * long click listener for Navigation Drawer */ - private class DrawerItemLongClickListener implements ListView.OnItemLongClickListener { + private class DrawerItemLongClickListener implements OnLongClickListener { @Override - public boolean onItemLongClick(AdapterView arg0, View arg1, final int position, long arg3) { + public boolean onLongClick(View v) { + int position = mDrawerListLeft.getChildAdapterPosition(v); showCloseDialog(position); return true; } @@ -1253,11 +1263,10 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements mIdGenerator++; mWebViewList.add(startingTab); - mTitleAdapter.notifyDataSetChanged(); if (show) { - mDrawerListLeft.setItemChecked(mWebViewList.size() - 1, true); showTab(startingTab); } + updateTabs(); new Handler().postDelayed(new Runnable() { @Override public void run() { @@ -1290,13 +1299,15 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements } if (current > position) { mWebViewList.remove(position); - mDrawerListLeft.setItemChecked(current - 1, true); + showTab(mWebViewList.get(current - 1)); + updateTabs(); reference.onDestroy(); } else if (mWebViewList.size() > position + 1) { if (current == position) { showTab(mWebViewList.get(position + 1)); mWebViewList.remove(position); - mDrawerListLeft.setItemChecked(position, true); + showTab(mWebViewList.get(position)); + updateTabs(); } else { mWebViewList.remove(position); } @@ -1306,7 +1317,8 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements if (current == position) { showTab(mWebViewList.get(position - 1)); mWebViewList.remove(position); - mDrawerListLeft.setItemChecked(position - 1, true); + showTab(mWebViewList.get(position - 1)); + updateTabs(); } else { mWebViewList.remove(position); } @@ -1322,12 +1334,11 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements reference.onDestroy(); mCurrentView = null; mWebView = null; - mTitleAdapter.notifyDataSetChanged(); + mTabAdapter.notifyDataSetChanged(); finish(); - } } - mTitleAdapter.notifyDataSetChanged(); + mTabAdapter.notifyDataSetChanged(); if (mIsNewIntent && isShown) { mIsNewIntent = false; @@ -1380,7 +1391,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements } } mWebViewList.clear(); - mTitleAdapter.notifyDataSetChanged(); + mTabAdapter.notifyDataSetChanged(); finish(); } @@ -1510,61 +1521,58 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements } } - public class LightningViewAdapter extends ArrayAdapter { + public class LightningViewAdapter extends RecyclerView.Adapter { - final Context context; - ColorMatrix colorMatrix; - ColorMatrixColorFilter filter; - Paint paint; - final int layoutResourceId; - List data = null; + private final Context context; + private final int layoutResourceId; + private List data = null; final CloseTabListener mExitListener; private final Drawable mBackgroundTabDrawable; private final Drawable mForegroundTabDrawable; + private final DrawerItemClickListener mClickListener; + private final DrawerItemLongClickListener mLongClickListener; + private ColorMatrix mColorMatrix; + private Paint mPaint; + private ColorFilter mFilter; + private static final float DESATURATED = 0.5f; public LightningViewAdapter(Context context, int layoutResourceId, List data) { - super(context, layoutResourceId, data); this.layoutResourceId = layoutResourceId; this.context = context; this.data = data; this.mExitListener = new CloseTabListener(); + this.mClickListener = new DrawerItemClickListener(); + this.mLongClickListener = new DrawerItemLongClickListener(); + if (mShowTabsInDrawer) { + mBackgroundTabDrawable = null; + mForegroundTabDrawable = ThemeUtils.getSelectedBackground(context, mDarkTheme); + } else { + int backgroundColor = Utils.mixTwoColors(ThemeUtils.getPrimaryColor(mActivity), Color.BLACK, 0.75f); + Bitmap backgroundTabBitmap = Bitmap.createBitmap(Utils.dpToPx(175), Utils.dpToPx(30), Bitmap.Config.ARGB_8888); + Utils.drawTrapezoid(new Canvas(backgroundTabBitmap), backgroundColor, true); + mBackgroundTabDrawable = new BitmapDrawable(getResources(), backgroundTabBitmap); - int backgroundColor = Utils.mixTwoColors(ThemeUtils.getPrimaryColor(mActivity), Color.BLACK, 0.75f); - Bitmap backgroundTabBitmap = Bitmap.createBitmap(Utils.dpToPx(175), Utils.dpToPx(30), Bitmap.Config.ARGB_8888); - Utils.drawTrapezoid(new Canvas(backgroundTabBitmap), backgroundColor, true); - mBackgroundTabDrawable = new BitmapDrawable(getResources(), backgroundTabBitmap); - - int foregroundColor = ThemeUtils.getPrimaryColor(context); - Bitmap foregroundTabBitmap = Bitmap.createBitmap(Utils.dpToPx(175), Utils.dpToPx(30), Bitmap.Config.ARGB_8888); - Utils.drawTrapezoid(new Canvas(foregroundTabBitmap), foregroundColor, false); - mForegroundTabDrawable = new BitmapDrawable(getResources(), foregroundTabBitmap); + int foregroundColor = ThemeUtils.getPrimaryColor(context); + Bitmap foregroundTabBitmap = Bitmap.createBitmap(Utils.dpToPx(175), Utils.dpToPx(30), Bitmap.Config.ARGB_8888); + Utils.drawTrapezoid(new Canvas(foregroundTabBitmap), foregroundColor, false); + mForegroundTabDrawable = new BitmapDrawable(getResources(), foregroundTabBitmap); + } } @Override - public View getView(final int position, View convertView, ViewGroup parent) { - View row = convertView; - LightningViewHolder holder; - if (row == null) { - LayoutInflater inflater = LayoutInflater.from(context); - row = inflater.inflate(layoutResourceId, parent, false); - - holder = new LightningViewHolder(); - holder.txtTitle = (TextView) row.findViewById(R.id.textTab); - holder.favicon = (ImageView) row.findViewById(R.id.faviconTab); - holder.exit = (ImageView) row.findViewById(R.id.deleteButton); - if (!mShowTabsInDrawer) { - holder.layout = (LinearLayout) row.findViewById(R.id.tab_item_background); - } - holder.exitButton = (FrameLayout) row.findViewById(R.id.deleteAction); - holder.exit.setColorFilter(mIconColor, PorterDuff.Mode.SRC_IN); - row.setTag(holder); - } else { - holder = (LightningViewHolder) row.getTag(); - } + public LightningViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { + LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext()); + View view = inflater.inflate(layoutResourceId, viewGroup, false); + return new LightningViewHolder(view); + } + @Override + public void onBindViewHolder(LightningViewHolder holder, int position) { holder.exitButton.setTag(position); holder.exitButton.setOnClickListener(mExitListener); + holder.layout.setOnClickListener(mClickListener); + holder.layout.setOnLongClickListener(mLongClickListener); ViewCompat.jumpDrawablesToCurrentState(holder.exitButton); @@ -1573,47 +1581,70 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements Bitmap favicon = web.getFavicon(); if (web.isForegroundTab()) { - holder.txtTitle.setTextAppearance(context, R.style.boldText); - holder.favicon.setImageBitmap(favicon); - if (!mShowTabsInDrawer) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - holder.layout.setBackground(mForegroundTabDrawable); - } else { - holder.layout.setBackgroundDrawable(mForegroundTabDrawable); - } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + holder.txtTitle.setTextAppearance(R.style.boldText); + } else { + holder.txtTitle.setTextAppearance(context, R.style.boldText); + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + holder.layout.setBackground(mForegroundTabDrawable); + } else { + holder.layout.setBackgroundDrawable(mForegroundTabDrawable); } if (!isIncognito() && mColorMode) { // TODO fix toolbar coloring // changeToolbarBackground(favicon, mForegroundTabDrawable); } + holder.favicon.setImageBitmap(favicon); } else { - holder.txtTitle.setTextAppearance(context, R.style.normalText); - if (!mShowTabsInDrawer) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - holder.layout.setBackground(mBackgroundTabDrawable); - } else { - holder.layout.setBackgroundDrawable(mBackgroundTabDrawable); - } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + holder.txtTitle.setTextAppearance(R.style.normalText); + } else { + holder.txtTitle.setTextAppearance(context, R.style.normalText); } - Bitmap grayscaleBitmap = Bitmap.createBitmap(favicon.getWidth(), - favicon.getHeight(), Bitmap.Config.ARGB_8888); - - Canvas c = new Canvas(grayscaleBitmap); - if (colorMatrix == null || filter == null || paint == null) { - paint = new Paint(); - colorMatrix = new ColorMatrix(); - colorMatrix.setSaturation(0.5f); - filter = new ColorMatrixColorFilter(colorMatrix); - paint.setColorFilter(filter); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + holder.layout.setBackground(mBackgroundTabDrawable); + } else { + holder.layout.setBackgroundDrawable(mBackgroundTabDrawable); } + holder.favicon.setImageBitmap(getDesaturatedBitmap(favicon)); + } + } + + @Override + public int getItemCount() { + return (data != null) ? data.size() : 0; + } - c.drawBitmap(favicon, 0, 0, paint); - holder.favicon.setImageBitmap(grayscaleBitmap); + public Bitmap getDesaturatedBitmap(Bitmap favicon) { + Bitmap grayscaleBitmap = Bitmap.createBitmap(favicon.getWidth(), + favicon.getHeight(), Bitmap.Config.ARGB_8888); + + Canvas c = new Canvas(grayscaleBitmap); + if (mColorMatrix == null || mFilter == null || mPaint == null) { + mPaint = new Paint(); + mColorMatrix = new ColorMatrix(); + mColorMatrix.setSaturation(DESATURATED); + mFilter = new ColorMatrixColorFilter(mColorMatrix); + mPaint.setColorFilter(mFilter); } - return row; + + c.drawBitmap(favicon, 0, 0, mPaint); + return grayscaleBitmap; } - class LightningViewHolder { + public class LightningViewHolder extends RecyclerView.ViewHolder { + + public LightningViewHolder(View view) { + super(view); + txtTitle = (TextView) view.findViewById(R.id.textTab); + favicon = (ImageView) view.findViewById(R.id.faviconTab); + exit = (ImageView) view.findViewById(R.id.deleteButton); + layout = (LinearLayout) view.findViewById(R.id.tab_item_background); + exitButton = (FrameLayout) view.findViewById(R.id.deleteAction); + exit.setColorFilter(mIconColor, PorterDuff.Mode.SRC_IN); + } + TextView txtTitle; ImageView favicon; ImageView exit; @@ -2024,58 +2055,13 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements } File bookmarkWebPage = new File(mActivity.getFilesDir(), BookmarkPage.FILENAME); - buildBookmarkPage(null, mBookmarkManager.getBookmarksFromFolder(null, true)); + BookmarkPage.buildBookmarkPage(this, null, mBookmarkManager.getBookmarksFromFolder(null, true)); view.loadUrl(Constants.FILE + bookmarkWebPage); } - private void buildBookmarkPage(final String folder, final List list) { - File bookmarkWebPage; - if (folder == null || folder.isEmpty()) { - bookmarkWebPage = new File(mActivity.getFilesDir(), BookmarkPage.FILENAME); - } else { - bookmarkWebPage = new File(mActivity.getFilesDir(), folder + '-' + BookmarkPage.FILENAME); - } - final StringBuilder bookmarkBuilder = new StringBuilder(BookmarkPage.HEADING); - - String folderIconPath = Constants.FILE + mActivity.getCacheDir() + "/folder.png"; - for (int n = 0; n < list.size(); n++) { - final HistoryItem item = list.get(n); - bookmarkBuilder.append(BookmarkPage.PART1); - if (item.isFolder()) { - File folderPage = new File(mActivity.getFilesDir(), item.getTitle() + '-' + BookmarkPage.FILENAME); - bookmarkBuilder.append(Constants.FILE).append(folderPage); - bookmarkBuilder.append(BookmarkPage.PART2); - bookmarkBuilder.append(folderIconPath); - new Thread(new Runnable() { - @Override - public void run() { - buildBookmarkPage(item.getTitle(), mBookmarkManager.getBookmarksFromFolder(item.getTitle(), true)); - } - }).run(); - } else { - bookmarkBuilder.append(item.getUrl()); - bookmarkBuilder.append(BookmarkPage.PART2).append(BookmarkPage.PART3); - bookmarkBuilder.append(item.getUrl()); - } - bookmarkBuilder.append(BookmarkPage.PART4); - bookmarkBuilder.append(item.getTitle()); - bookmarkBuilder.append(BookmarkPage.PART5); - } - bookmarkBuilder.append(BookmarkPage.END); - FileWriter bookWriter = null; - try { - bookWriter = new FileWriter(bookmarkWebPage, false); - bookWriter.write(bookmarkBuilder.toString()); - } catch (IOException e) { - e.printStackTrace(); - } finally { - Utils.close(bookWriter); - } - } - @Override - public void update() { - mTitleAdapter.notifyDataSetChanged(); + public void updateTabs() { + mTabAdapter.notifyDataSetChanged(); } /** diff --git a/app/src/main/java/acr/browser/lightning/constant/BookmarkPage.java b/app/src/main/java/acr/browser/lightning/constant/BookmarkPage.java index 20e0ff7..081bad1 100644 --- a/app/src/main/java/acr/browser/lightning/constant/BookmarkPage.java +++ b/app/src/main/java/acr/browser/lightning/constant/BookmarkPage.java @@ -3,8 +3,18 @@ */ package acr.browser.lightning.constant; +import android.app.Activity; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.List; + import acr.browser.lightning.R; import acr.browser.lightning.activity.BrowserApp; +import acr.browser.lightning.database.BookmarkManager; +import acr.browser.lightning.database.HistoryItem; +import acr.browser.lightning.utils.Utils; public class BookmarkPage { @@ -39,4 +49,50 @@ public class BookmarkPage { public static final String END = ""; + public static void buildBookmarkPage(final Activity activity, final String folder, final List list) { + final BookmarkManager manager = BookmarkManager.getInstance(activity); + File bookmarkWebPage; + if (folder == null || folder.isEmpty()) { + bookmarkWebPage = new File(activity.getFilesDir(), BookmarkPage.FILENAME); + } else { + bookmarkWebPage = new File(activity.getFilesDir(), folder + '-' + BookmarkPage.FILENAME); + } + final StringBuilder bookmarkBuilder = new StringBuilder(BookmarkPage.HEADING); + + String folderIconPath = Constants.FILE + activity.getCacheDir() + "/folder.png"; + for (int n = 0; n < list.size(); n++) { + final HistoryItem item = list.get(n); + bookmarkBuilder.append(BookmarkPage.PART1); + if (item.isFolder()) { + File folderPage = new File(activity.getFilesDir(), item.getTitle() + '-' + BookmarkPage.FILENAME); + bookmarkBuilder.append(Constants.FILE).append(folderPage); + bookmarkBuilder.append(BookmarkPage.PART2); + bookmarkBuilder.append(folderIconPath); + new Thread(new Runnable() { + @Override + public void run() { + buildBookmarkPage(activity, item.getTitle(), manager.getBookmarksFromFolder(item.getTitle(), true)); + } + }).run(); + } else { + bookmarkBuilder.append(item.getUrl()); + bookmarkBuilder.append(BookmarkPage.PART2).append(BookmarkPage.PART3); + bookmarkBuilder.append(item.getUrl()); + } + bookmarkBuilder.append(BookmarkPage.PART4); + bookmarkBuilder.append(item.getTitle()); + bookmarkBuilder.append(BookmarkPage.PART5); + } + bookmarkBuilder.append(BookmarkPage.END); + FileWriter bookWriter = null; + try { + bookWriter = new FileWriter(bookmarkWebPage, false); + bookWriter.write(bookmarkBuilder.toString()); + } catch (IOException e) { + e.printStackTrace(); + } finally { + Utils.close(bookWriter); + } + } + } diff --git a/app/src/main/java/acr/browser/lightning/constant/StartPage.java b/app/src/main/java/acr/browser/lightning/constant/StartPage.java index 5cfcf7d..50103cc 100644 --- a/app/src/main/java/acr/browser/lightning/constant/StartPage.java +++ b/app/src/main/java/acr/browser/lightning/constant/StartPage.java @@ -3,8 +3,16 @@ */ package acr.browser.lightning.constant; +import android.app.Activity; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + import acr.browser.lightning.activity.BrowserApp; import acr.browser.lightning.R; +import acr.browser.lightning.preference.PreferenceManager; +import acr.browser.lightning.utils.Utils; public class StartPage { @@ -38,4 +46,106 @@ public class StartPage { + "

"; + + /** + * This method builds the homepage and returns the local URL to be loaded + * when it finishes building. + * + * @return the URL to load + */ + public static String getHomepage(Activity activity) { + StringBuilder homepageBuilder = new StringBuilder(StartPage.HEAD); + String icon; + String searchUrl; + switch (PreferenceManager.getInstance().getSearchChoice()) { + case 0: + // CUSTOM SEARCH + icon = "file:///android_asset/lightning.png"; + searchUrl = PreferenceManager.getInstance().getSearchUrl(); + break; + case 1: + // GOOGLE_SEARCH; + icon = "file:///android_asset/google.png"; + // "https://www.google.com/images/srpr/logo11w.png"; + searchUrl = Constants.GOOGLE_SEARCH; + break; + case 2: + // ANDROID SEARCH; + icon = "file:///android_asset/ask.png"; + searchUrl = Constants.ASK_SEARCH; + break; + case 3: + // BING_SEARCH; + icon = "file:///android_asset/bing.png"; + // "http://upload.wikimedia.org/wikipedia/commons/thumb/b/b1/Bing_logo_%282013%29.svg/500px-Bing_logo_%282013%29.svg.png"; + searchUrl = Constants.BING_SEARCH; + break; + case 4: + // YAHOO_SEARCH; + icon = "file:///android_asset/yahoo.png"; + // "http://upload.wikimedia.org/wikipedia/commons/thumb/2/24/Yahoo%21_logo.svg/799px-Yahoo%21_logo.svg.png"; + searchUrl = Constants.YAHOO_SEARCH; + break; + case 5: + // STARTPAGE_SEARCH; + icon = "file:///android_asset/startpage.png"; + // "https://startpage.com/graphics/startp_logo.gif"; + searchUrl = Constants.STARTPAGE_SEARCH; + break; + case 6: + // STARTPAGE_MOBILE + icon = "file:///android_asset/startpage.png"; + // "https://startpage.com/graphics/startp_logo.gif"; + searchUrl = Constants.STARTPAGE_MOBILE_SEARCH; + break; + case 7: + // DUCK_SEARCH; + icon = "file:///android_asset/duckduckgo.png"; + // "https://duckduckgo.com/assets/logo_homepage.normal.v101.png"; + searchUrl = Constants.DUCK_SEARCH; + break; + case 8: + // DUCK_LITE_SEARCH; + icon = "file:///android_asset/duckduckgo.png"; + // "https://duckduckgo.com/assets/logo_homepage.normal.v101.png"; + searchUrl = Constants.DUCK_LITE_SEARCH; + break; + case 9: + // BAIDU_SEARCH; + icon = "file:///android_asset/baidu.png"; + // "http://www.baidu.com/img/bdlogo.gif"; + searchUrl = Constants.BAIDU_SEARCH; + break; + case 10: + // YANDEX_SEARCH; + icon = "file:///android_asset/yandex.png"; + // "http://upload.wikimedia.org/wikipedia/commons/thumb/9/91/Yandex.svg/600px-Yandex.svg.png"; + searchUrl = Constants.YANDEX_SEARCH; + break; + default: + // DEFAULT GOOGLE_SEARCH; + icon = "file:///android_asset/google.png"; + searchUrl = Constants.GOOGLE_SEARCH; + break; + + } + + homepageBuilder.append(icon); + homepageBuilder.append(StartPage.MIDDLE); + homepageBuilder.append(searchUrl); + homepageBuilder.append(StartPage.END); + + File homepage = new File(activity.getFilesDir(), StartPage.FILENAME); + FileWriter hWriter = null; + try { + hWriter = new FileWriter(homepage, false); + hWriter.write(homepageBuilder.toString()); + } catch (IOException e) { + e.printStackTrace(); + } finally { + Utils.close(hWriter); + } + + return Constants.FILE + homepage; + } } diff --git a/app/src/main/java/acr/browser/lightning/controller/BrowserController.java b/app/src/main/java/acr/browser/lightning/controller/BrowserController.java index 90ec2bc..b5d6afa 100644 --- a/app/src/main/java/acr/browser/lightning/controller/BrowserController.java +++ b/app/src/main/java/acr/browser/lightning/controller/BrowserController.java @@ -24,7 +24,7 @@ public interface BrowserController { void openFileChooser(ValueCallback uploadMsg); - void update(); + void updateTabs(); void onLongPress(); diff --git a/app/src/main/java/acr/browser/lightning/utils/ThemeUtils.java b/app/src/main/java/acr/browser/lightning/utils/ThemeUtils.java index e499eb3..054b59e 100644 --- a/app/src/main/java/acr/browser/lightning/utils/ThemeUtils.java +++ b/app/src/main/java/acr/browser/lightning/utils/ThemeUtils.java @@ -1,6 +1,7 @@ package acr.browser.lightning.utils; import android.content.Context; +import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapFactory; @@ -9,6 +10,7 @@ import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffColorFilter; +import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.os.Build; import android.support.annotation.AttrRes; @@ -44,10 +46,16 @@ public class ThemeUtils { } public static int getIconLightThemeColor(@NonNull Context context) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + return context.getResources().getColor(R.color.icon_light_theme, context.getTheme()); + } return context.getResources().getColor(R.color.icon_light_theme); } public static int getIconDarkThemeColor(@NonNull Context context) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + return context.getResources().getColor(R.color.icon_dark_theme, context.getTheme()); + } return context.getResources().getColor(R.color.icon_dark_theme); } @@ -70,7 +78,7 @@ public class ThemeUtils { } @Nullable - public static Drawable getThemedDrawable(@NonNull Context context, @DrawableRes int res, boolean dark){ + public static Drawable getThemedDrawable(@NonNull Context context, @DrawableRes int res, boolean dark) { int color = dark ? getIconDarkThemeColor(context) : getIconLightThemeColor(context); final Drawable drawable; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { @@ -86,7 +94,7 @@ public class ThemeUtils { } @Nullable - public static Drawable getLightThemedDrawable(@NonNull Context context, @DrawableRes int res){ + public static Drawable getLightThemedDrawable(@NonNull Context context, @DrawableRes int res) { final Drawable drawable; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { drawable = context.getResources().getDrawable(res); @@ -99,4 +107,17 @@ public class ThemeUtils { drawable.setColorFilter(getIconLightThemeColor(context), PorterDuff.Mode.SRC_IN); return drawable; } + + public static ColorDrawable getSelectedBackground(@NonNull Context context, boolean dark) { + Resources res = context.getResources(); + int color; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + color = (dark) ? res.getColor(R.color.divider_dark, context.getTheme()) : + res.getColor(R.color.divider_light, context.getTheme()); + } else { + color = (dark) ? res.getColor(R.color.divider_dark) : + res.getColor(R.color.divider_light); + } + return new ColorDrawable(color); + } } diff --git a/app/src/main/java/acr/browser/lightning/view/LightningView.java b/app/src/main/java/acr/browser/lightning/view/LightningView.java index 74eb04a..f3b8e1b 100644 --- a/app/src/main/java/acr/browser/lightning/view/LightningView.java +++ b/app/src/main/java/acr/browser/lightning/view/LightningView.java @@ -160,7 +160,7 @@ public class LightningView { return; } if (mHomepage.startsWith("about:home")) { - mWebView.loadUrl(getHomepage()); + mWebView.loadUrl(StartPage.getHomepage(mActivity)); } else if (mHomepage.startsWith("about:bookmarks")) { mBrowserController.openBookmarkPage(mWebView); } else { @@ -168,108 +168,6 @@ public class LightningView { } } - /** - * This method builds the homepage and returns the local URL to be loaded - * when it finishes building. - * - * @return the URL to load - */ - private String getHomepage() { - StringBuilder homepageBuilder = new StringBuilder(StartPage.HEAD); - String icon; - String searchUrl; - switch (mPreferences.getSearchChoice()) { - case 0: - // CUSTOM SEARCH - icon = "file:///android_asset/lightning.png"; - searchUrl = mPreferences.getSearchUrl(); - break; - case 1: - // GOOGLE_SEARCH; - icon = "file:///android_asset/google.png"; - // "https://www.google.com/images/srpr/logo11w.png"; - searchUrl = Constants.GOOGLE_SEARCH; - break; - case 2: - // ANDROID SEARCH; - icon = "file:///android_asset/ask.png"; - searchUrl = Constants.ASK_SEARCH; - break; - case 3: - // BING_SEARCH; - icon = "file:///android_asset/bing.png"; - // "http://upload.wikimedia.org/wikipedia/commons/thumb/b/b1/Bing_logo_%282013%29.svg/500px-Bing_logo_%282013%29.svg.png"; - searchUrl = Constants.BING_SEARCH; - break; - case 4: - // YAHOO_SEARCH; - icon = "file:///android_asset/yahoo.png"; - // "http://upload.wikimedia.org/wikipedia/commons/thumb/2/24/Yahoo%21_logo.svg/799px-Yahoo%21_logo.svg.png"; - searchUrl = Constants.YAHOO_SEARCH; - break; - case 5: - // STARTPAGE_SEARCH; - icon = "file:///android_asset/startpage.png"; - // "https://startpage.com/graphics/startp_logo.gif"; - searchUrl = Constants.STARTPAGE_SEARCH; - break; - case 6: - // STARTPAGE_MOBILE - icon = "file:///android_asset/startpage.png"; - // "https://startpage.com/graphics/startp_logo.gif"; - searchUrl = Constants.STARTPAGE_MOBILE_SEARCH; - break; - case 7: - // DUCK_SEARCH; - icon = "file:///android_asset/duckduckgo.png"; - // "https://duckduckgo.com/assets/logo_homepage.normal.v101.png"; - searchUrl = Constants.DUCK_SEARCH; - break; - case 8: - // DUCK_LITE_SEARCH; - icon = "file:///android_asset/duckduckgo.png"; - // "https://duckduckgo.com/assets/logo_homepage.normal.v101.png"; - searchUrl = Constants.DUCK_LITE_SEARCH; - break; - case 9: - // BAIDU_SEARCH; - icon = "file:///android_asset/baidu.png"; - // "http://www.baidu.com/img/bdlogo.gif"; - searchUrl = Constants.BAIDU_SEARCH; - break; - case 10: - // YANDEX_SEARCH; - icon = "file:///android_asset/yandex.png"; - // "http://upload.wikimedia.org/wikipedia/commons/thumb/9/91/Yandex.svg/600px-Yandex.svg.png"; - searchUrl = Constants.YANDEX_SEARCH; - break; - default: - // DEFAULT GOOGLE_SEARCH; - icon = "file:///android_asset/google.png"; - searchUrl = Constants.GOOGLE_SEARCH; - break; - - } - - homepageBuilder.append(icon); - homepageBuilder.append(StartPage.MIDDLE); - homepageBuilder.append(searchUrl); - homepageBuilder.append(StartPage.END); - - File homepage = new File(mActivity.getFilesDir(), StartPage.FILENAME); - FileWriter hWriter = null; - try { - hWriter = new FileWriter(homepage, false); - hWriter.write(homepageBuilder.toString()); - } catch (IOException e) { - e.printStackTrace(); - } finally { - Utils.close(hWriter); - } - - return Constants.FILE + homepage; - } - /** * Initialize the preference driven settings of the WebView * @@ -493,7 +391,7 @@ public class LightningView { public void setForegroundTab(boolean isForeground) { isForegroundTab = isForeground; - mBrowserController.update(); + mBrowserController.updateTabs(); } public boolean isForegroundTab() { @@ -758,7 +656,7 @@ public class LightningView { if (API >= android.os.Build.VERSION_CODES.KITKAT && mInvertPage) { view.evaluateJavascript(Constants.JAVASCRIPT_INVERT_PAGE, null); } - mBrowserController.update(); + mBrowserController.updateTabs(); } @Override @@ -769,7 +667,7 @@ public class LightningView { mBrowserController.showActionBar(); } mTitle.setFavicon(mWebpageBitmap); - mBrowserController.update(); + mBrowserController.updateTabs(); } @Override @@ -958,7 +856,7 @@ public class LightningView { if (icon == null) return; mTitle.setFavicon(icon); - mBrowserController.update(); + mBrowserController.updateTabs(); cacheFavicon(icon); } @@ -969,7 +867,7 @@ public class LightningView { } else { mTitle.setTitle(mActivity.getString(R.string.untitled)); } - mBrowserController.update(); + mBrowserController.updateTabs(); if (view != null) mBrowserController.updateHistory(title, view.getUrl()); } diff --git a/app/src/main/res/layout/tab_drawer.xml b/app/src/main/res/layout/tab_drawer.xml index bd6f3c5..3bbacc7 100644 --- a/app/src/main/res/layout/tab_drawer.xml +++ b/app/src/main/res/layout/tab_drawer.xml @@ -44,15 +44,13 @@ android:textAppearance="?android:attr/textAppearanceLarge" /> - + android:dividerHeight="0dp" /> + android:orientation="horizontal"> - - - - - + android:orientation="horizontal" + android:weightSum="1"> + + - + android:layout_marginLeft="16dp" + android:layout_marginRight="16dp" + android:layout_weight="1" + android:ellipsize="end" + android:fontFamily="sans-serif-light" + android:gravity="center_vertical" + android:maxLines="1" + android:minHeight="?android:attr/listPreferredItemHeightSmall" + android:singleLine="true" + android:textAppearance="?android:attr/textAppearanceListItemSmall" /> + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/tab_list_item_horizontal.xml b/app/src/main/res/layout/tab_list_item_horizontal.xml index d346057..5534d8f 100644 --- a/app/src/main/res/layout/tab_list_item_horizontal.xml +++ b/app/src/main/res/layout/tab_list_item_horizontal.xml @@ -5,6 +5,7 @@ android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="horizontal" + android:clickable="false" android:paddingLeft="10dp" android:paddingRight="10dp" android:weightSum="1"> diff --git a/app/src/main/res/layout/toolbar.xml b/app/src/main/res/layout/toolbar.xml index f1d32cb..9b144f3 100644 --- a/app/src/main/res/layout/toolbar.xml +++ b/app/src/main/res/layout/toolbar.xml @@ -8,18 +8,13 @@ android:elevation="2dp" android:orientation="vertical"> - -