Java tutorial
/* * Copyright (C) 2014 Ville Lehtiniemi * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package fi.villel.foosquare; import android.app.AlertDialog; import android.app.Dialog; import android.app.SearchManager; import android.content.DialogInterface; import android.content.Intent; import android.location.Location; import android.location.LocationManager; import android.os.Build; import android.provider.SearchRecentSuggestions; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.support.v7.widget.SearchView; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; import fi.villel.foosquare.model.SearchResult; import fi.villel.foosquare.presenter.Presenter; import fi.villel.foosquare.presenter.PresenterFactory; public class SearchActivity extends ActionBarActivity implements fi.villel.foosquare.view.SearchView { private final Presenter mPresenter = PresenterFactory.getPresenter(); private LocationManager mLocationManager; /** The last known location. */ private Location mLastLocation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); toolbar.setLogo(R.drawable.ic_launcher); toolbar.setLogoDescription(R.string.app_name); toolbar.setTitle(""); // Get the RecentQueryView and set the searchable configuration SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE); SearchView searchView = (SearchView) findViewById(R.id.search); // Assumes current activity is the searchable activity searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); if (savedInstanceState == null) { Fragment fragment = new ResultFragment(); getSupportFragmentManager().beginTransaction().add(R.id.result_container, fragment).commit(); } // TODO: Receive location updates mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE); mPresenter.onSearchViewCreate(this); handleIntent(getIntent()); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); handleIntent(intent); } @Override protected void onResume() { super.onResume(); // TODO: Receive periodic location updates // Right now the location is only updated when the view becomes visible updateLocation(); } @Override protected void onDestroy() { mPresenter.onSearchViewDestroy(this); super.onDestroy(); } private void handleIntent(Intent intent) { if (Intent.ACTION_SEARCH.equals(intent.getAction())) { String query = intent.getStringExtra(SearchManager.QUERY); mPresenter.startSearch(query, true); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_search, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); switch (id) { case R.id.action_clear_search_history: mPresenter.onClearSearchHistorySelected(); return true; case R.id.action_about: mPresenter.onAboutSelected(); return true; default: break; } return super.onOptionsItemSelected(item); } @Override public void saveRecentQuery(String query) { SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this, SearchSuggestionProvider.AUTHORITY, SearchSuggestionProvider.MODE); suggestions.saveRecentQuery(query, null); } @Override public void clearSearchHistory() { // Ask confirmation before clear FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.add(new ClearSearchHistoryDialog(), "ClearSearchHistoryDialog").commit(); } @Override public void showAboutDialog() { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.add(new AboutDialog(), "AboutDialog").commit(); } @Override public void showDetailsView(SearchResult result) { // TODO: Not implemented } @Override public void showErrorToast(String message, Throwable cause) { if (cause != null) { message += "\n" + cause; } Toast.makeText(this, message, Toast.LENGTH_LONG).show(); } private void updateLocation() { Location newLocation = null; Location gpsLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); Location networkLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (gpsLocation != null && networkLocation != null) { // Have both, choose the newer if (gpsLocation.getTime() >= networkLocation.getTime()) { newLocation = gpsLocation; } else { newLocation = networkLocation; } } else if (gpsLocation != null) { newLocation = gpsLocation; } else if (networkLocation != null) { newLocation = networkLocation; } if (newLocation != null) { double lat = newLocation.getLatitude(); double lon = newLocation.getLongitude(); mPresenter.setLocation(lat, lon); } } /** * Confirmation dialog before clearing the search history. */ public static class ClearSearchHistoryDialog extends DialogFragment { public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder b = new AlertDialog.Builder(getActivity()); b.setTitle(R.string.dialog_clear_search_history_title); b.setMessage(R.string.dialog_clear_search_history_message); b.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ClearSearchHistoryDialog.this.dismiss(); SearchRecentSuggestions suggestions = new SearchRecentSuggestions(getActivity(), SearchSuggestionProvider.AUTHORITY, SearchSuggestionProvider.MODE); suggestions.clearHistory(); } }); b.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ClearSearchHistoryDialog.this.dismiss(); } }); return b.create(); } } /** * The about dialog. */ public static class AboutDialog extends DialogFragment { public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder b = new AlertDialog.Builder(getActivity()); b.setTitle(R.string.dialog_about_title); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { b.setView(R.layout.about_dialog); } else { View view = getActivity().getLayoutInflater().inflate(R.layout.about_dialog, null); b.setView(view); } return b.create(); } } }