Back to project page Rhybudd.
The source code is released under:
GNU General Public License
If you think the Android project Rhybudd listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (C) 2013 - Gareth Llewellyn *//from w ww. j a va2s. c o m * This file is part of Rhybudd - http://blog.NetworksAreMadeOfString.co.uk/Rhybudd/ * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see <http://www.gnu.org/licenses/> */ package net.networksaremadeofstring.rhybudd; import java.util.ArrayList; import java.util.List; import android.app.ActionBar; import android.app.Activity; import android.app.ProgressDialog; import android.app.SearchManager; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.preference.PreferenceManager; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.widget.Button; import android.widget.ListView; import android.widget.EditText; import android.widget.Toast; import com.bugsense.trace.BugSenseHandler; public class Search extends Activity { //private SharedPreferences settings = null; ListView list; String query, index; Handler searchResultsHandler; ArrayList<String> SearchResults = new ArrayList<String>(); List<ZenossDevice> listOfZenossDevices = new ArrayList<ZenossDevice>(); Animation anim; ActionBar actionbar; ProgressDialog dialog; int DeviceCount = 0; SharedPreferences settings = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { settings = PreferenceManager.getDefaultSharedPreferences(this); } catch (Exception e) { BugSenseHandler.sendExceptionMessage("Search", "onCreate", e); } setContentView(R.layout.search); try { actionbar = getActionBar(); actionbar.setDisplayHomeAsUpEnabled(true); actionbar.setHomeButtonEnabled(true); } catch (Exception e) { BugSenseHandler.sendExceptionMessage("Search", "onCreate", e); } list = (ListView)findViewById(R.id.searchResultsListView); ((Button) findViewById(R.id.SearchButton)).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { query = ((EditText) findViewById(R.id.searchTermEditText)).getText().toString(); ConfigureHandler(); PerformSearch(true); } } ); try { Intent intent = getIntent(); if (Intent.ACTION_SEARCH.equals(intent.getAction())) { //((RelativeLayout) findViewById(R.id.searchContainer)).setVisibility(8); query = intent.getStringExtra(SearchManager.QUERY); ((EditText) findViewById(R.id.searchTermEditText)).setText(query); index = "device"; ConfigureHandler(); PerformSearch(false); } else { //((ProgressBar) findViewById(R.id.progressBar1)).setVisibility(4); //((TextView) findViewById(R.id.CurrentTaskLabel)).setVisibility(4); } } catch (Exception e) { //TODO We should probably do something actually useful here BugSenseHandler.sendExceptionMessage("Search", "onCreate", e); } } private void ConfigureHandler() { searchResultsHandler = new Handler() { public void handleMessage(Message msg) { if(dialog != null && dialog.isShowing()) dialog.dismiss(); /*if(msg.what == 99) { listOfZenossDevices.clear(); ((TextView)findViewById(R.id.CurrentTaskLabel)).setVisibility(0); ((ProgressBar) findViewById(R.id.progressBar1)).setVisibility(0); } else */if (msg.what == 1) { try { Toast.makeText(Search.this, "An error was encountered;\r\n" + msg.getData().getString("exception"), Toast.LENGTH_LONG).show(); } catch (Exception e) { BugSenseHandler.sendExceptionMessage("Search", "ConfigureHandler", e); } } else { try { if(null != listOfZenossDevices && listOfZenossDevices.size() > 0) { list.setAdapter(new ZenossSearchAdaptor(Search.this, listOfZenossDevices)); } else { Toast.makeText(Search.this, "No matches found", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { BugSenseHandler.sendExceptionMessage("Search", "ConfigureHandler", e); } } } }; } public void PerformSearch(Boolean Crafted) { try { dialog = new ProgressDialog(this); dialog.setTitle("Contacting Zenoss"); dialog.setMessage("Please wait:\nLoading Infrastructure...."); dialog.setCancelable(false); dialog.show(); } catch (Exception e) { BugSenseHandler.sendExceptionMessage("Search", "PerformSearch", e); } Thread dataPreload = new Thread() { public void run() { if(null != listOfZenossDevices) listOfZenossDevices.clear(); try { //listOfZenossDevices = rhybuddCache.SearchRhybuddDevices(query); RhybuddDataSource datasource = new RhybuddDataSource(Search.this); datasource.open(); listOfZenossDevices = datasource.SearchRhybuddDevices(query); datasource.close(); } catch(Exception e) { BugSenseHandler.sendExceptionMessage("Search", "PerformSearch", e); listOfZenossDevices.clear(); } if(listOfZenossDevices!= null && listOfZenossDevices.size() > 0) { DeviceCount = listOfZenossDevices.size(); //Log.i("DeviceList","Found DB Data!"); searchResultsHandler.sendEmptyMessage(0); } else { //Log.i("DeviceList","No DB data found, querying API directly"); Message msg = new Message(); Bundle bundle = new Bundle(); bundle.putString("exception","A query to the local DB returned no devices"); msg.setData(bundle); msg.what = 1; searchResultsHandler.sendMessage(msg); //TODO Query the API directly /*if(listOfZenossDevices != null) listOfZenossDevices.clear(); try { ZenossAPIv2 API = new ZenossAPIv2(settings.getString("userName", ""), settings.getString("passWord", ""), settings.getString("URL", "")); listOfZenossDevices = API.GetRhybuddDevices(); if(listOfZenossDevices != null && listOfZenossDevices.size() > 0) { DeviceCount = listOfZenossDevices.size(); searchResultsHandler.sendEmptyMessage(1); rhybuddCache.UpdateRhybuddDevices(listOfZenossDevices); } else { Message msg = new Message(); Bundle bundle = new Bundle(); bundle.putString("exception","A query to both the local DB and Zenoss API returned no devices"); msg.setData(bundle); msg.what = 99; searchResultsHandler.sendMessage(msg); } } catch (Exception e) { BugSenseHandler.log("DeviceList", e); Message msg = new Message(); Bundle bundle = new Bundle(); bundle.putString("exception",e.getMessage()); msg.setData(bundle); msg.what = 99; searchResultsHandler.sendMessage(msg); } searchResultsHandler.sendEmptyMessage(0);*/ } } }; dataPreload.start(); } public void ViewDevice(String UID) { try { Intent ViewDeviceIntent = new Intent(Search.this, ViewZenossDeviceActivity.class); ViewDeviceIntent.putExtra(ViewZenossDeviceFragment.ARG_UID, UID); startActivity(ViewDeviceIntent); } catch (Exception e) { BugSenseHandler.sendExceptionMessage("Search", "ConfigureHandler", e); } } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: { //No need for crazy intents finish(); return true; } default: return super.onOptionsItemSelected(item); } } }