# [MOD] ICS Browser Maximum open tab setting



## gdanko (Jun 25, 2011)

Mod Type:: Application

Difficulty:: Easy

Mod Base:: AOSP

Mod Status:: Stable

Apply In:: ADB

Carrier:: N/A

Requires Root:: Yes

Android Version:: 4.0.4 (ICS)

Source:: https://github.com/gdanko/android_packages_apps_Browser/commit/994084e5c3b6217cdd090984b3253cf48178c674
I got tired of being told I am out of tabs and having to go close them so I modified the stock ICS browser to make this a setting.

I first added a menu item to the browser called "Tab settings"








There is one option in there, "Maximum open tabs"








When you select this option, you get a dialog with the possible options.








You can download the file here: http://dl.dropbox.co..._nexus_cdma.zip

If you want to build it yourself, this is how you do it. The following HOWTO assumes you have a working build environment and knowledge of how to build Android. I will be including this mod into Codename Android but it will work with AOKP, CM, or any other AOSP variant.

Each section will contain the filename in bold, and explanation, and finally the code.

*packages/apps/Browser/res/values/integers.xml*
max_tabs is no longer needed so you can remove the following two lines.

```
<br />
<!--  The maximum number of open tabs --><br />
<integer name="max_tabs">16</integer><br />
```
*packages/apps/Browser/res/values/strings.xml*
You want to put this code at the end of the file. It simply defines the strings we'll use for the mod.

```
<br />
<!-- Title for tab settings preference --><br />
<string name="tab_settings_title">Tab settings</string><br />
<!-- Dialog title --><br />
<string name="pref_max_open_tabs">Maximum open tabs</string><br />
<!-- Summary for Maximum open tabs --><br />
<string name="pref_max_open_tabs_summary"><br />
	Configure the maximum allowable open tabs for the browser</string><br />
<!-- Options in the Maximum open tabs dialog box --><br />
<string-array name="pref_max_open_tabs_choices"><br />
	<item>5</item><br />
	<item>10</item><br />
	<item>15</item><br />
	<item>20</item><br />
	<item>25</item><br />
	<item>30</item><br />
</string-array><br />
<!-- Do not translate --><br />
<string-array name="pref_max_open_tabs_values"><br />
	<item>5</item><br />
	<item>10</item><br />
	<item>15</item><br />
	<item>20</item><br />
	<item>25</item><br />
	<item>30</item><br />
</string-array><br />
<br />
<!-- Title for Restore tabs --><br />
<string name="pref_restore_tabs">Restore tabs</string><br />
<!-- Summary for Restore tabs --><br />
<string name="pref_restore_tabs_summary">Restore tabs from your previous session on browser launch</string><br />
```
*packages/apps/Browser/res/xml/preference_headers.xml*
Here we'll create the new menu entry in the Browser settings. Put this at the end of the file, after the last header directive.

```
<br />
<header android:fragment="com.android.browser.preferences.TabsPreferencesFragment"<br />
	android:title="@string/tab_settings_title"<br />
/><br />
```
*packages/apps/Browser/src/com/android/browser/Controller.java*
This is a very important step. Each time the browser requests a new tab, getMaxTabs() is called to pass back the maximum number of allowable open tabs. Previously this was a hard-coded integer but now it needs to be read from preferences. Before being able to read the preferences we need to import a couple of things. Put these at the top of the file with the other imports.

```
<br />
import android.content.SharedPreferences;<br />
import android.preference.PreferenceManager;<br />
```
In this section we're going to modify getMaxTabs() to read the value from Preferences. Find the declaration for the getMaxTabs method and modify it to look like this.

```
<br />
int getMaxTabs() {<br />
	Context mContext = mActivity.getApplicationContext();<br />
	SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);<br />
	String numTabs = mPrefs.getString(PreferenceKeys.PREF_MAX_TABS, "20");<br />
	int t = Integer.parseInt(numTabs);<br />
	return t;<br />
}<br />
```
In this section the preferences for the restore tab checkbox is read in and if it is false, restoreState is ignored.
Now find the section for onPreloginFinished and make it look like this.

```
<br />
	private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,<br />
			boolean restoreIncognitoTabs, boolean fromCrash) {<br />
<br />
		Context mContext = mActivity.getApplicationContext();<br />
		SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);<br />
		Boolean restoreTabs = mPrefs.getBoolean(PreferenceKeys.PREF_RESTORE_TABS, true);<br />
```
Now after the } else { statement, add this.

```
<br />
if(restoreTabs == true) {<br />
```
And close it right before after the last }. You should indent everything inside of this if then statement.

*packages/apps/Browser/src/com/android/browser/PreferenceKeys.java*
Here we're going to add a constant for this setting. This can go at the end of the file, after the last entry.

```
<br />
// ----------------------<br />
// Keys for tabs_preferences.xml<br />
// ----------------------<br />
static final String PREF_MAX_TABS = "max_open_tabs";<br />
static final String PREF_RESTORE_TABS = "restore_tabs_on_launch";<br />
```
*packages/apps/Browser/src/com/android/browser/TabControl.java*
This is another important section. Since the previous behavior was to have a hard-coded value for max tabs, the variable mMaxTabs was read in once at browser launch and never needed to be re-read. We have to change canCreateNewTab to read from the preferences instead of using the static value. Find the canCreateNewTabs method and make it look like this.

```
<br />
boolean canCreateNewTab() {<br />
	return mController.getMaxTabs() > mTabs.size();<br />
}<br />
```
*Two new files need to be created.*
These files set up the "Maximum open tabs" menu item and actually handle updating the preferences when you select an option.

*packages/apps/Browser/res/xml/tabs_preferences.xml*

```
<br />
<?xml version="1.0" encoding="utf-8"?><br />
<!-- Copyright (C) 2008 The Android Open Source Project<br />
	 Licensed under the Apache License, Version 2.0 (the "License");<br />
	 you may not use this file except in compliance with the License.<br />
	 You may obtain a copy of the License at<br />
		  [URL=http://www.apache.org/licenses/LICENSE-2]http://www.apache.org/licenses/LICENSE-2[/URL].0<br />
	 Unless required by applicable law or agreed to in writing, software<br />
	 distributed under the License is distributed on an "AS IS" BASIS,<br />
	 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.<br />
	 See the License for the specific language governing permissions and<br />
	 limitations under the License.<br />
--><br />
<PreferenceScreen<br />
xmlns:android="http://schemas.android.com/apk/res/android" ><br />
<ListPreference<br />
	android:key="max_open_tabs"<br />
	android:defaultValue="20"<br />
	android:title="@string/pref_max_open_tabs"<br />
	android:summary="@string/pref_max_open_tabs_summary"<br />
	android:entries="@array/pref_max_open_tabs_choices"<br />
	android:entryValues="@array/pref_max_open_tabs_values" /><br />
<CheckBoxPreference<br />
	   	android:key="restore_tabs_on_launch"<br />
		[URL=android:defaultValue]android:defaultValue[/URL] = "true"<br />
		android:title="@string/pref_restore_tabs"<br />
		android:summary="@string/pref_restore_tabs_summary" /><br />
</PreferenceScreen><br />
```
*packages/apps/Browser/src/com/android/browser/preferences/TabsPreferencesFragment.java*

```
<br />
/*<br />
* Copyright (C) 2010 The Android Open Source Project<br />
*<br />
* Licensed under the Apache License, Version 2.0 (the "License");<br />
* you may not use this file except in compliance with the License.<br />
* You may obtain a copy of the License at<br />
*<br />
*	  [URL=http://www.apache.org/licenses/LICENSE-2]http://www.apache.org/licenses/LICENSE-2[/URL].0<br />
*<br />
* Unless required by applicable law or agreed to in writing, software<br />
* distributed under the License is distributed on an "AS IS" BASIS,<br />
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.<br />
* See the License for the specific language governing permissions and<br />
* limitations under the License<br />
*/<br />
package com.android.browser.preferences;<br />
import android.os.Bundle;<br />
import android.preference.Preference;<br />
import android.preference.PreferenceFragment;<br />
import com.android.browser.BrowserSettings;<br />
import com.android.browser.PreferenceKeys;<br />
import com.android.browser.R;<br />
import com.android.browser.search.SearchEngine;<br />
public class TabsPreferencesFragment extends PreferenceFragment {<br />
	@Override<br />
	public void onCreate(Bundle savedInstanceState) {<br />
		super.onCreate(savedInstanceState);<br />
		// Load the XML preferences file<br />
		addPreferencesFromResource(R.xml.tabs_preferences);<br />
	}<br />
}<br />
```
Once you have done all of this you can go to the root level of your AOSP source tree and type

```
<br />
make Browser<br />
```
This will take a few minutes. Once it is finished you will see something like this in the terminal:

```
<br />
target Dex: Browser<br />
Copying: out/target/common/obj/APPS/Browser_intermediates/proguard.classes.dex<br />
target Package: Browser (out/target/product/toro/obj/APPS/Browser_intermediates/package.apk)<br />
aapt: warning: string 'pref_default_text_encoding_default' in packages/apps/Browser/res marked untranslatable but exists in locale 'ja'<br />
'out/target/common/obj/APPS/Browser_intermediates/classes.dex' as 'classes.dex'...<br />
Install: out/target/product/toro/system/app/Browser.apk<br />
```
You will need to back up the old Browser.apk in /system/app. You can do this by re-mounting your system partition as rw. The actual command varies by device so you will need to look it up on the internet. Once you have re-mounted the system partition rw you can back up the old Browser.apk with:

```
<br />
cd /system/app<br />
mv Browser.apk Browser.apk.old<br />
```
Last of all you can push the newly built Browser.apk to /system/app

```
<br />
adb push /path/to/Browser.apk /system/app<br />
```
I will make an update.zip shortly but for now this will get you going. I've tested this on a CDMA Galaxy Nexus and an Incredible 2. Both worked perfectly.


----------



## Mustang302LX (Jun 28, 2011)

Wow, thanks for all this!


----------



## gdanko (Jun 25, 2011)

No problem. Like I said, it's about sharing and improving out platform.


----------



## gdanko (Jun 25, 2011)

I am thinking of adding a couple things.
1) An option under Tab settings to clear all tabs immediately. Or maybe under the main browser menu?
2) An option that says something like, "If I change my maximum to something LOWER than the current, clear the oldest tabs... i.e.,

If my current max is 5 and I have the following 5 tabs loaded:
1) www.apple.com
2) www.google.com
3) www.yahoo.com
4) www.rootzwiki.com
5) www.microsoft.com

And I switch my max to 3, the oldest two would be shaved off and I would now have:
1) www.yahoo.com
2) www.rootzwiki.com
3) www.microsoft.com

I wonder how useful or not that would be.


----------



## gdanko (Jun 25, 2011)

I added to this mod. By default, the browser saves your state (open tabs, etc) between reboots and launches. I don't necessarily like this on a mobile browser. So voila, it's now a setting. I will post the code HOWTO later tonight.


----------



## gdanko (Jun 25, 2011)

Here is an update.zip file for the CDMA Galaxy Nexus
http://dl.dropbox.com/u/6995251/max_open_tab_browser_mod/gdanko_browser_mod_galaxy_nexus_cdma.zip

If you want one for your device, PM me the output from mount and I will build a package.


----------

