예제 #1
0
/**
 * MakeCursorAdapter
 *
 * @author Remel Pugh
 * @version 1.0
 */
@SuppressWarnings("unused")
public class MakeCursorAdapter extends CursorAdapter {
  private static final String TAG = Logger.makeTag(MakeCursorAdapter.class);
  private final LayoutInflater inflater;
  private ContentResolver resolver;

  /**
   * @param context
   * @param c
   */
  @SuppressWarnings("deprecation")
  public MakeCursorAdapter(final Context context, final Cursor c) {
    super(context, c);

    inflater = LayoutInflater.from(context);
    resolver = context.getContentResolver();
  }

  /**
   * @param context
   * @param c
   * @param autoRequery
   */
  public MakeCursorAdapter(final Context context, final Cursor c, final boolean autoRequery) {
    super(context, c, autoRequery);

    inflater = LayoutInflater.from(context);
    resolver = context.getContentResolver();
  }

  /**
   * @param context
   * @param c
   * @param flags
   */
  public MakeCursorAdapter(final Context context, final Cursor c, final int flags) {
    super(context, c, flags);

    inflater = LayoutInflater.from(context);
    resolver = context.getContentResolver();
  }

  /** {@inheritDoc} */
  @Override
  public void bindView(final View view, final Context context, final Cursor cursor) {
    final ViewsFinder finder = new ViewsFinder(view);
    final TextView text = finder.find(id.text1);

    text.setText(convertToString(cursor));
  }

  /** {@inheritDoc} */
  @Override
  public CharSequence convertToString(final Cursor cursor) {
    return cursor.getString(Columns.MAKE_NAME.getIndex());
  }

  /** {@inheritDoc} */
  @Override
  public View newView(final Context context, final Cursor cursor, final ViewGroup parent) {
    return inflater.inflate(android.R.layout.simple_dropdown_item_1line, parent, false);
  }

  /** {@inheritDoc} */
  @Override
  public Cursor runQueryOnBackgroundThread(final CharSequence constraint) {
    if (getFilterQueryProvider() != null) {
      return getFilterQueryProvider().runQuery(constraint);
    }

    final String sort;
    final String selection;
    final String[] selectionArg;

    sort = Columns.MAKE_NAME.getName() + " COLLATE NOCASE ASC";
    selection = constraint == null ? null : Columns.MAKE_NAME.getName() + " LIKE ?";
    selectionArg = constraint == null ? null : new String[] {"%" + constraint.toString() + "%"};

    return resolver.query(Make.CONTENT_URI, Make.PROJECTION, selection, selectionArg, sort);
  }
}
/**
 * BaseDialogFragment
 *
 * @author Remel Pugh
 * @version 1.0
 */
@SuppressWarnings("unused")
public abstract class BaseDialogFragment extends DialogFragment implements FragmentBase {
  private static final String TAG = Logger.makeTag(BaseDialogFragment.class);
  @Inject @ForActivity protected Context activityContext;
  @Inject @ForApplication protected Context applicationContext;
  protected ViewsFinder finder;
  private boolean isDualPane = false;
  private boolean isFirstAttach = true;

  /** {@inheritDoc} */
  @Override
  public boolean isDualPane() {
    return isDualPane;
  }

  /** {@inheritDoc} */
  @Override
  public void setDualPane(final boolean isDualPane) {
    this.isDualPane = isDualPane;
  }

  /** {@inheritDoc} */
  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);

    // activityContext = activity;
    if (getActivity() instanceof BaseActivity && isFirstAttach) {
      ((BaseActivity) getActivity()).inject(this);

      isFirstAttach = false;
    }
  }

  /** {@inheritDoc} */
  @Override
  public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    initialize(savedInstanceState);
    setHasOptionsMenu(getMenuResourceId() != null && getMenuResourceId() > 0);
  }

  /** {@inheritDoc} */
  @Override
  public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
    if (hasOptionsMenu() && getMenuResourceId() != null) {
      inflater.inflate(getMenuResourceId(), menu);

      super.onCreateOptionsMenu(menu, inflater);
    }
  }

  /** {@inheritDoc} */
  @Override
  public View onCreateView(
      final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
    final View view = inflater.inflate(getLayoutResourceId(), null);

    finder = new ViewsFinder(view);

    afterViews(savedInstanceState);

    return view;
  }

  /** {@inheritDoc} */
  @Override
  public void onDetach() {
    activityContext = null;
    applicationContext = null;

    super.onDetach();
  }

  /** {@inheritDoc} */
  @Override
  public void onViewCreated(final View view, final Bundle savedInstanceState) {
    if (getDialog() != null) {
      getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }

    super.onViewCreated(view, savedInstanceState);
  }

  /** {@inheritDoc} */
  @Override
  public void setSubtitle(final int subtitleResId) {
    setSubtitle(getString(subtitleResId));
  }

  /** {@inheritDoc} */
  @Override
  public void setSubtitle(final String subtitle) {
    if (!isAdded()) {
      return;
    }

    if (getDialog() == null) {
      if (getActivity() instanceof ActionBarActivity) {
        ((ActionBarActivity) getActivity()).getSupportActionBar().setSubtitle(subtitle);
      }
    }
  }

  /** {@inheritDoc} */
  @Override
  public void setTitle(final int titleId) {
    setTitle(getString(titleId));
  }

  /** {@inheritDoc} */
  @Override
  public void setTitle(final String title) {
    if (!isAdded()) {
      return;
    }

    if (getDialog() == null) {
      getActivity().setTitle(title);
    } else {
      getDialog().setTitle(title);
    }
  }

  /**
   * Called after the view has been created.
   *
   * @param savedInstanceState The saved state of fragment.
   */
  protected abstract void afterViews(final Bundle savedInstanceState);

  /**
   * Gets the layout resource for this instance.
   *
   * @return Resource identifier for the layout resource.
   */
  protected abstract int getLayoutResourceId();

  /**
   * Gets the menu resource for this instance.
   *
   * @return Resource identifier for the menu resource.
   */
  protected abstract Integer getMenuResourceId();

  /**
   * Initializes this instance.
   *
   * @param savedInstanceState If the fragment is being re-initialized after previously being shut
   *     down then this Bundle contains the data it most recently supplied in {@link
   *     BaseDialogFragment#onSaveInstanceState(android.os.Bundle)}. Note: Otherwise it is null.
   */
  @SuppressWarnings("EmptyMethod")
  protected void initialize(final Bundle savedInstanceState) {}
}
/** @author Remel Pugh */
@SuppressWarnings("unused")
public abstract class BaseRecyclerActivity extends ActionBarActivity implements Injector {
  private static final String TAG = Logger.makeTag(BaseRecyclerActivity.class);
  protected Bundle activityState;
  protected ViewsFinder finder;
  protected FragmentFinder fragmentFinder;
  private ObjectGraph activityGraph;
  private RecyclerView.Adapter<?> adapter;
  private RecyclerView recyclerView;
  private Toolbar toolbar;

  /** {@inheritDoc} */
  @Override
  public ObjectGraph getObjectGraph() {
    return activityGraph;
  }

  /** {@inheritDoc} */
  @Override
  public void inject(Object target) {
    // ensure object graph has been created
    buildGraph();

    activityGraph.inject(target);
  }

  /** {@inheritDoc} */
  @Override
  public boolean onCreateOptionsMenu(final Menu menu) {
    final Integer resourceId = getMenuResource();

    if (resourceId != null) {
      getMenuInflater().inflate(getMenuResource(), menu);
    }

    return true;
  }

  /** {@inheritDoc} */
  @Override
  public boolean onOptionsItemSelected(final MenuItem item) {
    final int id = item.getItemId();

    switch (id) {
      case android.R.id.home:
        {
          navigateUp();

          return true;
        }
      default:
        {
          return super.onOptionsItemSelected(item);
        }
    }
  }

  /** {@inheritDoc} */
  @Override
  public void setContentView(final int layoutResID) {
    super.setContentView(layoutResID);

    afterSetContentView();
  }

  /** {@inheritDoc} */
  @Override
  public void setContentView(final View view) {
    super.setContentView(view);

    afterSetContentView();
  }

  /** {@inheritDoc} */
  @Override
  public void setContentView(final View view, final ViewGroup.LayoutParams params) {
    super.setContentView(view, params);

    afterSetContentView();
  }

  /** */
  protected void afterSetContentView() {
    final RecyclerView.ItemAnimator animator;

    finder = new ViewsFinder(this);
    fragmentFinder = new FragmentFinder(this);

    recyclerView = finder.find(R.id.util__recycler_view);
    if (recyclerView == null) {
      throw new IllegalStateException(
          "Unable to find a RecyclerView with id of @id/util__recycler_view");
    }

    recyclerView.setLayoutManager(getLayoutManager());
    recyclerView.setAdapter(getAdapter());

    animator = getItemAnimator();
    if (animator != null) {
      recyclerView.setItemAnimator(animator);
    }

    afterViews(activityState);
  }

  /** Executed after the activity's layout has been assigned */
  protected abstract void afterViews(Bundle savedInstanceState);

  /** @return */
  protected abstract List<Object> getActivityModules();

  /** @return */
  protected abstract RecyclerView.Adapter<?> getAdapter();

  /** @return */
  protected abstract RecyclerView.ItemAnimator getItemAnimator();

  protected abstract RecyclerView.LayoutManager getLayoutManager();

  /**
   * Identifies the layout resource to be used by the current activity.
   *
   * @return The resource identifier for the layout
   */
  protected Integer getLayoutResource() {
    return R.layout.util__activity_recycler_view;
  }

  /**
   * Identifies the menu resource to be used by the current activity.
   *
   * @return The resource identifier for the menu
   */
  protected Integer getMenuResource() {
    return null;
  }

  /** @return */
  protected Toolbar getToolbar() {
    return toolbar;
  }

  /** @param savedInstanceState */
  protected void initialize(final Bundle savedInstanceState) {
    activityState = savedInstanceState;
  }

  /** @return */
  protected boolean isHomeAsUpEnabled() {
    return false;
  }

  /** @return */
  protected boolean isHomeButtonEnabled() {
    return true;
  }

  /** @return */
  protected boolean isLogoEnabled() {
    return false;
  }

  /**
   * Determines if the application title is displayed by default it is not displayed.
   *
   * @return true if the title is to be displayed, otherwise false
   */
  protected boolean isTitleEnabled() {
    return false;
  }

  /** */
  protected void navigateUp() {
    NavigationUtils.navigateUp(this);
  }

  /** Sets the default configuration for the activity {@link android.support.v7.app.ActionBar}. */
  protected void onConfigureActionBar() {
    final ActionBar actionBar = getSupportActionBar();

    actionBar.setDisplayHomeAsUpEnabled(isHomeAsUpEnabled());
    actionBar.setDisplayShowTitleEnabled(isTitleEnabled());
    actionBar.setDisplayUseLogoEnabled(isLogoEnabled());
    actionBar.setHomeButtonEnabled(isHomeButtonEnabled());
  }

  /** {@inheritDoc} */
  @Override
  protected void onCreate(final Bundle savedInstanceState) {
    buildGraph();

    initialize(savedInstanceState);

    super.onCreate(savedInstanceState);

    if (getLayoutResource() == null) {
      throw new IllegalStateException("A layout must be specified.");
    }

    setContentView(getLayoutResource());
    setToolbar();
    onConfigureActionBar();
  }

  /** {@inheritDoc} */
  @Override
  protected void onDestroy() {
    // Eagerly clear the reference to the activity graph to allow it to be garbage collected as soon
    // as possible.
    activityGraph = null;

    super.onDestroy();
  }

  /** */
  protected void setToolbar() {
    toolbar = finder.find(R.id.util__toolbar);
    if (toolbar != null) {
      setSupportActionBar(toolbar);
    }
  }

  private void buildGraph() {
    if (activityGraph == null) {
      final ApplicationExtension application = (ApplicationExtension) getApplication();
      final List<Object> modules = CollectionUtils.newList();

      // Create the activity graph by .plus-ing our modules onto the application graph.
      modules.add(new ActivityModule(this));
      modules.addAll(getActivityModules());

      activityGraph = application.getObjectGraph().plus(modules.toArray());

      // Inject ourselves so subclasses will have dependencies fulfilled when this method returns.
      activityGraph.inject(this);
    }
  }
}
예제 #4
0
/**
 * VehicleCursorAdapter
 *
 * @author Remel Pugh
 * @version 1.0
 */
@SuppressWarnings("unused")
public class VehicleCursorAdapter extends BaseCheckableCursorAdapter {
  private static final String TAG = Logger.makeTag(VehicleCursorAdapter.class);

  /** {@inheritDoc} */
  public VehicleCursorAdapter(
      final Context context, final Bundle savedInstanceState, final Cursor c) {
    super(context, savedInstanceState, R.layout.list_item_vehicle, c);
  }

  /** {@inheritDoc} */
  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  public VehicleCursorAdapter(
      final Context context, final Bundle savedInstanceState, final Cursor c, final int flags) {
    super(context, savedInstanceState, R.layout.list_item_vehicle, c, flags);
  }

  /** {@inheritDoc} */
  @Override
  public boolean onActionItemClicked(final ActionMode mode, final MenuItem item) {
    final int id = item.getItemId();

    if (id == R.id.action_delete) {
      if (getOnActionModeCallbackListener() != null) {
        getOnActionModeCallbackListener().onActionDelete(mode);
      }

      return true;
    }

    return false;
  }

  /** {@inheritDoc} */
  @Override
  public boolean onCreateActionMode(final ActionMode mode, final Menu menu) {
    final MenuInflater inflater = ((FragmentActivity) helper.getContext()).getMenuInflater();

    if (getOnActionModeCallbackListener() != null) {
      getOnActionModeCallbackListener().onActionModeCreated(mode);
    }

    inflater.inflate(R.menu.menu_delete_contextual, menu);

    return true;
  }

  /** {@inheritDoc} */
  @Override
  public boolean onPrepareActionMode(final ActionMode mode, final Menu menu) {
    return false;
  }

  /** {@inheritDoc} */
  @Override
  protected void populateData(final Context context, final View view, final Cursor cursor) {
    final ViewsFinder finder = new ViewsFinder(view);
    final TextView vehicleName = finder.find(id.text1);
    final TextView makeModel = finder.find(id.text2);
    final StringBuilder text = new StringBuilder();
    final VehicleDTO vehicle = VehicleDTO.newInstance(cursor);

    if (vehicle.getYear() > 0) {
      text.append(vehicle.getYear()).append(" ");
    }

    text.append(vehicle.getMakeName()).append(" ").append(vehicle.getModelName());

    vehicleName.setText(vehicle.getName());
    makeModel.setText(text);
  }
}