Android Open Source - Rhybudd Search






From Project

Back to project page Rhybudd.

License

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.

Java Source Code

/*
 * 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);
      }
  }
}




Java Source Code List

net.networksaremadeofstring.rhybudd.AddDeviceActivity.java
net.networksaremadeofstring.rhybudd.AddDeviceFragment.java
net.networksaremadeofstring.rhybudd.AuthenticatorService.java
net.networksaremadeofstring.rhybudd.Authenticator.java
net.networksaremadeofstring.rhybudd.CoreSettingsFragment.java
net.networksaremadeofstring.rhybudd.DeviceListWelcomeFragment.java
net.networksaremadeofstring.rhybudd.DeviceList.java
net.networksaremadeofstring.rhybudd.DiagnosticActivity.java
net.networksaremadeofstring.rhybudd.EventsListWelcomeFragment.java
net.networksaremadeofstring.rhybudd.FirstRunSettings.java
net.networksaremadeofstring.rhybudd.GCMIntentService.java
net.networksaremadeofstring.rhybudd.ManageDatabase.java
net.networksaremadeofstring.rhybudd.ManageUpdate.java
net.networksaremadeofstring.rhybudd.MassAcknowledgeReceiver.java
net.networksaremadeofstring.rhybudd.Notifications.java
net.networksaremadeofstring.rhybudd.PushConfigActivity.java
net.networksaremadeofstring.rhybudd.PushSettingsFragment.java
net.networksaremadeofstring.rhybudd.RhybuddBackupAgent.java
net.networksaremadeofstring.rhybudd.RhybuddDataSource.java
net.networksaremadeofstring.rhybudd.RhybuddDock.java
net.networksaremadeofstring.rhybudd.RhybuddDream.java
net.networksaremadeofstring.rhybudd.RhybuddHandlers.java
net.networksaremadeofstring.rhybudd.RhybuddHome.java
net.networksaremadeofstring.rhybudd.RhybuddOpenHelper.java
net.networksaremadeofstring.rhybudd.Search.java
net.networksaremadeofstring.rhybudd.SettingsFragment.java
net.networksaremadeofstring.rhybudd.StubProvider.java
net.networksaremadeofstring.rhybudd.SwipeDismissListViewTouchListener.java
net.networksaremadeofstring.rhybudd.SwipeDismissTouchListener.java
net.networksaremadeofstring.rhybudd.SyncAdapter.java
net.networksaremadeofstring.rhybudd.SyncService.java
net.networksaremadeofstring.rhybudd.TrustAllManager.java
net.networksaremadeofstring.rhybudd.TrustAllSSLSocketFactory.java
net.networksaremadeofstring.rhybudd.URLDrawable.java
net.networksaremadeofstring.rhybudd.URLImageParser.java
net.networksaremadeofstring.rhybudd.ViewEventFragment.java
net.networksaremadeofstring.rhybudd.ViewZenossDeviceActivity.java
net.networksaremadeofstring.rhybudd.ViewZenossDeviceFragment.java
net.networksaremadeofstring.rhybudd.ViewZenossDeviceListActivity.java
net.networksaremadeofstring.rhybudd.ViewZenossDeviceListFragment.java
net.networksaremadeofstring.rhybudd.ViewZenossDevice.java
net.networksaremadeofstring.rhybudd.ViewZenossEventActivity.java
net.networksaremadeofstring.rhybudd.ViewZenossEventFragment.java
net.networksaremadeofstring.rhybudd.ViewZenossEvent.java
net.networksaremadeofstring.rhybudd.ViewZenossEventsListActivity.java
net.networksaremadeofstring.rhybudd.ViewZenossEventsListFragment.java
net.networksaremadeofstring.rhybudd.ViewZenossGroupsActivity.java
net.networksaremadeofstring.rhybudd.ViewZenossGroupsFragment.java
net.networksaremadeofstring.rhybudd.WriteNFCActivity.java
net.networksaremadeofstring.rhybudd.ZaasSettingsFragment.java
net.networksaremadeofstring.rhybudd.ZenPack.java
net.networksaremadeofstring.rhybudd.ZenossAPICore.java
net.networksaremadeofstring.rhybudd.ZenossAPIZaas.java
net.networksaremadeofstring.rhybudd.ZenossAPI.java
net.networksaremadeofstring.rhybudd.ZenossAPIv2.java
net.networksaremadeofstring.rhybudd.ZenossCredentials.java
net.networksaremadeofstring.rhybudd.ZenossDeviceAdaptor.java
net.networksaremadeofstring.rhybudd.ZenossDevice.java
net.networksaremadeofstring.rhybudd.ZenossEvent.java
net.networksaremadeofstring.rhybudd.ZenossEventsAdaptor.java
net.networksaremadeofstring.rhybudd.ZenossGCMBroadcastReceiver.java
net.networksaremadeofstring.rhybudd.ZenossGroupsGridAdapter.java
net.networksaremadeofstring.rhybudd.ZenossPoller.java
net.networksaremadeofstring.rhybudd.ZenossSearchAdaptor.java
net.networksaremadeofstring.rhybudd.ZenossWidgetGraph.java
net.networksaremadeofstring.rhybudd.ZenossWidget.java