Skip to content
This repository has been archived by the owner on Dec 19, 2022. It is now read-only.

Commit

Permalink
Bug 1022105 - Support a menu and settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
EricEdens committed Jul 25, 2014
1 parent 0653cce commit 8d97eea
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 14 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Expand Up @@ -28,6 +28,8 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
// You must install or update the Support Repository through the SDK manager to use this dependency.
compile 'com.android.support:appcompat-v7:19.+'
// You must install or update the Support Repository through the SDK manager to use this dependency.
compile 'com.android.support:support-v4:19.+'
}
16 changes: 14 additions & 2 deletions app/src/main/java/org/mozilla/search/PreSearchFragment.java
Expand Up @@ -5,6 +5,7 @@
package org.mozilla.search;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
Expand Down Expand Up @@ -76,7 +77,10 @@ public void onDestroy() {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
listView = (ListView) inflater.inflate(R.layout.search_fragment_pre_search, container, false);
final View mainView = inflater.inflate(R.layout.search_fragment_pre_search, container, false);

// Initialize listview.
listView = (ListView) mainView.findViewById(R.id.list_view);
listView.setAdapter(cursorAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
Expand All @@ -91,7 +95,15 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
}
}
});
return listView;

// Apply click handler to settings button.
mainView.findViewById(R.id.settings_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(), SearchPreferenceActivity.class));
}
});
return mainView;
}

@Override
Expand Down
96 changes: 96 additions & 0 deletions app/src/main/java/org/mozilla/search/SearchPreferenceActivity.java
@@ -0,0 +1,96 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.search;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.widget.Toast;

import org.mozilla.gecko.db.BrowserContract;

/**
* This activity allows users to modify the settings for the search activity.
*
* A note on implementation: At the moment, we don't have tablet-specific designs.
* Therefore, this implementation uses the old-style PreferenceActivity. When
* we start optimizing for tablets, we can migrate to Fennec's PreferenceFragment
* implementation.
*
* TODO: Change this to PreferenceFragment when we stop supporting devices older than SDK 11.
*/
public class SearchPreferenceActivity extends PreferenceActivity {

private static final String LOGTAG = "SearchPreferenceActivity";

private static final String CLEAR_SEARCH_HISTORY_BUTTON_KEY = "clear_search_history_button";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if (getActionBar() != null) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
setupPrefsScreen();
}

@SuppressWarnings("deprecation")
private void setupPrefsScreen() {
addPreferencesFromResource(R.xml.search_preferences);

final Preference clearHistoryButton = findPreference(CLEAR_SEARCH_HISTORY_BUTTON_KEY);
clearHistoryButton.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(SearchPreferenceActivity.this);
dialogBuilder.setNegativeButton(android.R.string.cancel, null);
dialogBuilder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
clearHistory();
}
});
dialogBuilder.setMessage(R.string.search_pref_clear_history_dialog_message);
dialogBuilder.show();
return false;
}
});
}

private void clearHistory() {
final AsyncTask<Void, Void, Boolean> clearHistoryTask = new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... params) {
final int numDeleted = getContentResolver().delete(
BrowserContract.SearchHistory.CONTENT_URI, null, null);
return numDeleted >= 0;
}

@Override
protected void onPostExecute(Boolean success) {
if (success) {
getContentResolver().notifyChange(BrowserContract.SearchHistory.CONTENT_URI, null);
Toast.makeText(SearchPreferenceActivity.this, SearchPreferenceActivity.this.getResources()
.getString(R.string.search_pref_clear_history_confirmation), Toast.LENGTH_SHORT).show();
} else {
Log.e(LOGTAG, "Error clearing search history.");
}
}
};
clearHistoryTask.execute();
}
}
Expand Up @@ -38,7 +38,7 @@ public boolean onCreate() {

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
throw new UnsupportedOperationException("Not yet implemented");
return 1;
}

@Override
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 21 additions & 4 deletions app/src/main/res/layout/search_fragment_pre_search.xml
Expand Up @@ -2,9 +2,26 @@
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<ListView
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:dividerHeight="0dp"/>
android:layout_height="match_parent">

<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:dividerHeight="0dp"/>

<ImageButton
android:id="@+id/settings_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:padding="15dp"
android:src="@drawable/ic_action_overflow"
android:text="@string/search_settings_icon"
android:layout_gravity="bottom|right"/>

</FrameLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values-v13/search_styles.xml
Expand Up @@ -10,4 +10,6 @@
<item name="android:colorBackground">@color/global_background_color</item>
</style>

<style name="SettingsTheme" parent="@android:style/Theme.Holo.Light"/>

</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/search_styles.xml
Expand Up @@ -10,4 +10,6 @@
<item name="android:colorBackground">@color/global_background_color</item>
</style>

<style name="SettingsTheme" parent="@android:style/Theme.Light"/>

</resources>
9 changes: 9 additions & 0 deletions app/src/main/res/xml/search_preferences.xml
@@ -0,0 +1,9 @@
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:key="clear_search_history_button"
android:title="@string/search_pref_clear_history_title"/>
</PreferenceScreen>
14 changes: 10 additions & 4 deletions manifests/SearchAndroidManifest_activities.xml.in
Expand Up @@ -3,10 +3,6 @@
android:label="@string/search_app_name"
android:theme="@style/AppTheme"
android:screenOrientation="portrait">

<!-- Add this to activity declaration to hide keyboard on launch -->
<!-- android:windowSoftInputMode="stateHidden" -->

<intent-filter>
<action android:name="android.intent.action.MAIN"/>

Expand All @@ -18,3 +14,13 @@
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>

<activity
android:name="org.mozilla.search.SearchPreferenceActivity"
android:label="@string/search_pref_title"
android:parentActivityName="org.mozilla.search.MainActivity"
android:theme="@style/SettingsTheme" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="org.mozilla.search.MainActivity"/>
</activity>
8 changes: 7 additions & 1 deletion strings/search_strings.dtd
Expand Up @@ -3,6 +3,12 @@
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<!ENTITY search_jump_arrow '&#8598;'>
<!ENTITY search_settings_icon '&#8942;'>

<!ENTITY search_app_name 'Firefox Search'>
<!ENTITY search_header_image_content_description 'Firefox Search Header Image'>
<!ENTITY search_for_something 'Search for something'>

<!ENTITY search_pref_title 'Settings'>
<!ENTITY search_pref_clear_history_confirmation 'History cleared'>
<!ENTITY search_pref_clear_history_dialog_message 'Delete all search history from this device?'>
<!ENTITY search_pref_clear_history_title 'Clear search history'>
10 changes: 8 additions & 2 deletions strings/search_strings.xml.in
@@ -1,4 +1,10 @@
<string name="search_app_name">&search_app_name;</string>
<string name="search_jump_arrow">&search_jump_arrow;</string>
<string name="search_header_image_content_description">&search_header_image_content_description;</string>
<string name="search_settings_icon">&search_settings_icon;</string>

<string name="search_app_name">&search_app_name;</string>
<string name="search_for_something">&search_for_something;</string>

<string name="search_pref_title">&search_pref_title;</string>
<string name="search_pref_clear_history_confirmation">&search_pref_clear_history_confirmation;</string>
<string name="search_pref_clear_history_dialog_message">&search_pref_clear_history_dialog_message;</string>
<string name="search_pref_clear_history_title">&search_pref_clear_history_title;</string>

0 comments on commit 8d97eea

Please sign in to comment.