Android Open Source - FlibityBoop Search Activity






From Project

Back to project page FlibityBoop.

License

The source code is released under:

Apache License

If you think the Android project FlibityBoop 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

/**
 * IFT2905 : Interface personne machine// w w  w  . ja va2  s.co m
 * Projet de session: FlibityBoop.
 * Team: Vincent CABELI, Henry LIM, Pamela MEHANNA, Emmanuel NOUTAHI, Olivier TASTET
 * @author Emmanuel Noutahi, Vincent Cabeli
 */

package com.maclandrol.flibityboop;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import com.maclandrol.flibityboop.API.MediaType;

import android.app.Activity;
import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcelable;
import android.preference.PreferenceManager;
import android.provider.SearchRecentSuggestions;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.ToggleButton;

/**
 * Activit SearchActivity
 * Affiche es rsultats des diffrentes requtes de recherche effectues
 * utilise newIntent pour trouver dterminer quel type de recherche  effectuer
 */
public class SearchActivity extends BaseActivity {

  private ListView myList;
  private ArrayList<? extends MediaInfos> filminfosList;
  private ArrayList<? extends MediaInfos> showinfosList;
  private ArrayList<? extends MediaInfos> mediainfosList;
  final static int TV_ON_AIR=7;
  final static int TV_AIRING_TODAY=8;
  final static int MOVIE_UPCOMMING=4;
  final static int MOVIE_IN_THEATHER=5;
  final static int POPULAR=1;
  final static int TOP_RATED=2;
  // Number of results returned by APIs
  private int nResultsRT;
  private int nResultsTTV;
  private int maxPage;

  private boolean includeMovies = true;
  private boolean includeShows = true;

  private MediaAdapter mAdapter;
  private ProgressDialog progress;
  private ProgressBar toggleProgress;
  private Activity activity;
  private String previous_query = "", search_title="";
  private TextView noResultTextView;

  public void onNewIntent(Intent intent) {
    noResultTextView.setVisibility(View.INVISIBLE);
    myList.setVisibility(View.VISIBLE);
    setIntent(intent);
    handleIntent(intent);
  }

  private void handleIntent(Intent intent) {

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
      //intent de type recherche

      boolean from_main = intent.getBooleanExtra("drawer", false);
      boolean from_genre = intent.getBooleanExtra("from_genre", false);
      String new_query = intent.getStringExtra(SearchManager.QUERY);

      //Intent du mainActivity (popular, topRated, ...)
      if(from_main){
        previous_query = new_query;
        Utils.setLastQuery(new_query);
        int type=intent.getIntExtra("type",0);
        new DrawerAsyncTask(type).execute();
      }
      
      //Intent du MainActivity mais avec les recherches de films par genre
      else if (from_genre){
        previous_query = new_query;
        Utils.setLastQuery(new_query);
        String genre = intent.getStringExtra("genre");
        new GenreAsyncTask(genre).execute();
      }

      //Intent avec recherche par query
      else{
  
       if (!previous_query.equalsIgnoreCase(new_query)) {

        previous_query = new_query;
        Utils.setLastQuery(new_query);

        new DownloadLoginTask().execute(previous_query);

        SearchRecentSuggestions suggestions = new SearchRecentSuggestions(
            this, SearchSuggestionProvider.AUTHORITY,
            SearchSuggestionProvider.MODE);
        suggestions.saveRecentQuery(previous_query, null);
      }
      }
    } else {
      //Sinon pas de recherche, l'appel est deja avec les donnes de recherche, juste afficher ces donnes
      Bundle b = intent.getBundleExtra("Similar");

      String origin = null;
      try {
        origin = b.getString("origin");
      } catch (Exception e) {
        e.printStackTrace();
      }
      if ("noRequest".equals(origin)) {

        filminfosList = b.getParcelableArrayList("movies");
        showinfosList = b.getParcelableArrayList("shows");
        mediainfosList = Utils.entrelace(filminfosList, showinfosList);
        Log.d("search", ""+mediainfosList.size());
        this.search_title = "Recommendations for \""
            + b.getString("titre") + "\"";
        displaySearchResult();
      }
    }

  }

  /*
   * Click sur un item du listview de search doit amener vers la page de dtails
   */
  private class ListOnItemClick implements OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> adapter, View view,
        int position, long id) {
      Intent i = new Intent(SearchActivity.this, MediaDetails.class);
      MediaInfos to_send=null;
      if(includeMovies && includeShows)
        to_send=mediainfosList.get(position);
      else if(includeMovies)
        to_send=filminfosList.get(position);
      else
        to_send=showinfosList.get(position);
      i.putExtra("mediainfo",(Parcelable)to_send);
      startActivity(i);

    }
  }


  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("origin","noRequest");
    outState.putParcelableArrayList("movies", filminfosList);
    outState.putParcelableArrayList("shows", showinfosList);
    outState.putBoolean("isSaved", true);
    outState.putString("search_title", this.search_title);

  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    activity = this;
    //Utiliser les donnes de l'utilisateur pour le nombre de rsultat et le nombre de page
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    nResultsRT= Integer.parseInt(sharedPref.getString("max_req", "20"));
    maxPage=Integer.parseInt(sharedPref.getString("maxPage", "1"));
    nResultsTTV= Integer.parseInt(sharedPref.getString("max_req", "20"));
    setContentView(R.layout.activity_search);
    myList = (ListView) findViewById(R.id.searchList);
    myList.setOnItemClickListener(new ListOnItemClick());
    toggleProgress = (ProgressBar) findViewById(R.id.searchToggleProgress2);
    progress = new ProgressDialog(activity);
    progress.setCancelable(true);
    progress.setMessage("Searching for your media");
    progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progress.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
          dialog.dismiss();
        }
    });
    
    noResultTextView = (TextView) activity.findViewById(R.id.noResult);
    
    //Ne pas refaire des requtes si onSaveInstance a t appel
    if(savedInstanceState!=null && savedInstanceState.getBoolean("search_title")){
      this.filminfosList=savedInstanceState.getParcelableArrayList("movies");
      this.showinfosList=savedInstanceState.getParcelableArrayList("shows");
      this.search_title=savedInstanceState.getString("search_title");
      this.displaySearchResult();
    }
    
    else{
      Intent i = getIntent();
      onNewIntent(i);
    }
    
  }

  
  @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) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
      NavUtils.navigateUpFromSameTask(this);
      return true;
    }
    return super.onOptionsItemSelected(item);

  }

  /*
   * Affichage des rsultats de recherche en fonction des filtres
   */
  private void displaySearchResult() {

    ArrayList<? extends MediaInfos> listToDisplay = null;
    noResultTextView.setVisibility(View.INVISIBLE);
    toggleProgress.setVisibility(View.VISIBLE);

    if (includeMovies && !includeShows)
      listToDisplay = filminfosList;

    else if (includeShows && !includeMovies)
      listToDisplay = showinfosList;

    else if (includeShows && includeMovies) {
      listToDisplay = mediainfosList;
    }

    else {
      myList.setAdapter(null);
      toggleProgress.setVisibility(View.INVISIBLE);
      return;
      }

    if (listToDisplay == null || listToDisplay.isEmpty()) {
      myList.setAdapter(null);
      // myList.setVisibility(View.INVISIBLE);
      String noResult = (getString(R.string.noResult))
          + " \""
          + previous_query
          + "\" "
          + (includeMovies ? ((includeShows) ? " in movies and TV shows"
              : "in movies")
              : ((includeShows) ? "in TV shows" : ""));

      noResultTextView.setText(noResult);
      noResultTextView.setVisibility(View.VISIBLE);
    }

    else {
      mAdapter = new MediaAdapter(getApplicationContext(), listToDisplay);
      myList.setAdapter(mAdapter);
    }

    toggleProgress.setVisibility(View.INVISIBLE);
    ((TextView)findViewById(R.id.search_text)).setText(this.search_title);
    progress.dismiss();
  }



  // Movie Toggle
  public void onMovieToggleClicked(View view) {
    boolean on = ((ToggleButton) view).isChecked();

    if (on) {
      includeMovies = true;
    } else {
      includeMovies = false;
    }
    displaySearchResult();
  }

  // Show toggle
  public void onShowToggleClicked(View view) {
    boolean on = ((ToggleButton) view).isChecked();

    if (on) {
      includeShows = true;
    } else {
      includeShows = false;
    }
    displaySearchResult();
  }

  /*
   * AsyncTask pour les recherches de mdia bas sur un query
   */
  protected class DownloadLoginTask extends AsyncTask<String, String, Void> {

    String query;

    protected void onPreExecute() {

      Log.d("work", "PrexExecute()");

      progress.show();
    }

    protected Void doInBackground(String... params) {

      //Chercher  la fois les films et les TVShows. L'utilisateur pourrait trier plus tard
      RottenTomatoes RT = new RottenTomatoes();
      TraktTV TTV = new TraktTV();
      query = params[0];

      ArrayList<RTSearch> a = null;
      ArrayList<TraktTVSearch> b = null;
      try {
        a = RT.searchMovies(query, nResultsRT, maxPage);
      } catch (Exception e) {
        Log.e("asyncError", e.getMessage());
      }
      try {
        b = TTV.searchShow(query, nResultsTTV);
      } catch (Exception e) {
        Log.e("asyncError", e.getMessage());
      }
      filminfosList = a;
      showinfosList = b;
      Log.d("async", "making mediainfoslists");
      //Mettre les films et les show de facon alternative
      mediainfosList = Utils.entrelace(filminfosList, showinfosList);

      return null;
    }

    protected void onPostExecute(Void result) {
      search_title="Search result for \""+query+"\"";
      displaySearchResult();//afficher les rsultats  la fin

    }

  }
  
  /*
   * AsyncTask pour les recherches de films bas sur le genre (Intent from MainActivity)
   */
  protected class GenreAsyncTask extends AsyncTask<String, String, Void> {

    String genre="";
    
    public GenreAsyncTask (String genre){
      this.genre = genre;
    }
    
    protected void onPreExecute() {
      progress.show();
    }
    
    protected Void doInBackground(String... params) {

      TheMovieDB tmdb=new TheMovieDB();

      ArrayList<TMDBSearch> a = null;
      
      try {
        a = tmdb.getMoviesByGenres(genre, false, maxPage);

      } catch (Exception e) {
        Log.e("asyncError", e.getMessage());
      }
      
      filminfosList = a;
      mediainfosList = a;

      return null;
    }
    

    protected void onPostExecute(Void result) {
      search_title = genre.substring(0, 1).toUpperCase() + genre.substring(1) + " movies";
      displaySearchResult();

    }

  }
  
  /*
   * AsyncTask pour les recherches de mdia  bas  sur les options du drawer du MainActivity
   */
  protected class DrawerAsyncTask extends AsyncTask<Void, String, Void> {

    int queryType; //type de requte  faire
    String message="";
    boolean movie=true; //seulement de type film
    boolean mixte=false; //la recherche est mixte (popular ou TopRated)
    public DrawerAsyncTask (int queryType){
      this.queryType=queryType;
    }
    
    protected void onPreExecute() {

      Log.d("work", "PrexExecute()");
      progress.show();
    }

    protected Void doInBackground(Void... params) {

      TheMovieDB tmdb=new TheMovieDB();

      ArrayList<TMDBSearch> a = null;
      try {
        switch(queryType){
        case SearchActivity.MOVIE_IN_THEATHER:
          a= tmdb.getInTheaterMovies(maxPage);
          this.message="Movies in theather ";
          movie=true;
          break;
        case SearchActivity.MOVIE_UPCOMMING:
          a= tmdb.getUpcomingMovies(maxPage);
          this.message="Upcoming movies";
          movie=true;
          break;
        case SearchActivity.TV_AIRING_TODAY:
          a= tmdb.getAiringToday(maxPage);
          this.message="TV Show Airing today";
          movie=false;
          break;
          
        case SearchActivity.TV_ON_AIR:
          this.message="TVShow on air";
          a=tmdb.getOnAirTV(maxPage);
          movie=false;
          break;
        case SearchActivity.POPULAR:
          this.message="Really populars now";
          a=tmdb.getPopularMedia(MediaType.Any, maxPage);
          mixte=true;
          break;
          
        case SearchActivity.TOP_RATED:
          this.message="Top rated media";
          a=tmdb.getTopRatedMedia(MediaType.Any, maxPage);
          mixte=true;
          break;
          
        }
        
      } catch (Exception e) {
        Log.e("asyncError", e.getMessage());
    
      }
      
      //Distribuer les rsulats correctement dans filminfosList et showinfosList
      if (mixte) {
        filminfosList = new ArrayList<TMDBSearch>(a);
        showinfosList = new ArrayList<TMDBSearch>(a);
        Set<MediaInfos> removeShow = new HashSet<MediaInfos>();
        Set<MediaInfos> removeFilm = new HashSet<MediaInfos>();
        for (MediaInfos m : a) {
          if (m.isShow()){
            removeShow.add(m);
          }
          else
            removeFilm.add(m);
        }
        filminfosList.removeAll(removeShow);
        showinfosList.removeAll(removeFilm);
        mediainfosList = Utils.entrelace(filminfosList, showinfosList);
      } else {
        mediainfosList = a;

        if (movie) {
          filminfosList = a;
        } else
          showinfosList = a;
      }
      Log.d("async", "making mediainfoslists");
      return null;
    }

    protected void onPostExecute(Void result) {
      search_title=this.message;
      displaySearchResult();

    }


  }


}




Java Source Code List

com.maclandrol.flibityboop.API.java
com.maclandrol.flibityboop.BaseActivity.java
com.maclandrol.flibityboop.Critics.java
com.maclandrol.flibityboop.DBHelperMedia.java
com.maclandrol.flibityboop.FavoriteActivity.java
com.maclandrol.flibityboop.FileCache.java
com.maclandrol.flibityboop.ImageLoader.java
com.maclandrol.flibityboop.MainActivity.java
com.maclandrol.flibityboop.MediaAdapter.java
com.maclandrol.flibityboop.MediaContentProvider.java
com.maclandrol.flibityboop.MediaDetails.java
com.maclandrol.flibityboop.MediaInfos.java
com.maclandrol.flibityboop.Media.java
com.maclandrol.flibityboop.MemoryCache.java
com.maclandrol.flibityboop.MovieFavoriteCursorAdapter.java
com.maclandrol.flibityboop.MovieListFragment.java
com.maclandrol.flibityboop.RottenTomatoes.java
com.maclandrol.flibityboop.SearchActivity.java
com.maclandrol.flibityboop.SearchSuggestionProvider.java
com.maclandrol.flibityboop.SectionListAdapter.java
com.maclandrol.flibityboop.SettingActivity.java
com.maclandrol.flibityboop.ShowFavoriteCursorAdapter.java
com.maclandrol.flibityboop.TVListFragment.java
com.maclandrol.flibityboop.Tastekid.java
com.maclandrol.flibityboop.TheMovieDB.java
com.maclandrol.flibityboop.TraktTV.java
com.maclandrol.flibityboop.Utils.java