Java tutorial
/** * RSSin - Clever RSS reader for Android * Copyright (C) 2015 Randy Wanga, Jos Craaijo, Joep Bernards, Camil Staps * * 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 2 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, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ package org.rssin.android; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import org.rssin.rssin.Filter; import org.rssin.rssin.R; import java.io.IOException; import java.util.List; /** * @author Jos. */ public class NavigationDrawerManageFiltersFragment extends Fragment { private View rootView; /** * Returns a new instance of this fragment for the given section * number. */ public static NavigationDrawerManageFiltersFragment newInstance() { NavigationDrawerManageFiltersFragment fragment = new NavigationDrawerManageFiltersFragment(); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { container.removeAllViews(); rootView = inflater.inflate(R.layout.fragment_filters, container, false); final Context context = rootView.getContext(); filtersView = (ListView) rootView.findViewById(R.id.filters_list); try { filtersList = FiltersList.getInstance(context); } catch (IOException ex) { Frontend.error(context, R.string.error_load_filters, ex); } filterAdapter = new FilterAdapter(context, R.layout.item_filter, filtersList.getFilters()); filtersView.setAdapter(filterAdapter); setupListeners(); return rootView; } @Override public void onAttach(Activity activity) { super.onAttach(activity); } private FiltersList filtersList; private ListView filtersView; private FilterAdapter filterAdapter; private AdapterView.OnItemClickListener onFilterClickListener; private AdapterView.OnItemLongClickListener onFilterLongClickListener; @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.filters_action_add) { openAddDialog(); return true; } return super.onOptionsItemSelected(item); } /** * Setup listeners for the list items */ private void setupListeners() { onFilterClickListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Filter item = (Filter) parent.getItemAtPosition(position); openFilterSettings(item); } }; filtersView.setOnItemClickListener(onFilterClickListener); } /** * Open dialog to create new filter * For the moment, we temporarily disable rotating because we can't get it working otherwise. * Possible feature: make rotating possible */ public void openAddDialog() { //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); AlertDialog.Builder alert = new AlertDialog.Builder(rootView.getContext()); alert.setTitle(getString(R.string.filters_action_add)); alert.setMessage(getString(R.string.filter_settings_title)); final EditText input = new EditText(rootView.getContext()); input.setFocusable(true); input.requestFocus(); AlertDialog dialog = alert.setView(input).setPositiveButton(getResources().getString(R.string.button_apply), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String value = input.getText().toString(); try { Filter f = new Filter(value); filtersList.getFilters().add(f); filtersList.save(); filterAdapter.notifyDataSetChanged(); openFilterSettings(f); } catch (Exception e) { Frontend.error(rootView.getContext(), R.string.error_save_filters, e); } //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); } }).setNegativeButton(getResources().getString(R.string.button_cancel), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); } }) .setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); } }).create(); dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); dialog.show(); } public void openFilterSettings(Filter f) { f.ensureFeeds(DefaultStorageProvider.getInstance(rootView.getContext())); Intent intent = new Intent(rootView.getContext().getApplicationContext(), FilterSettingsActivity.class); intent.putExtra("filter", f.hashCode()); startActivity(intent); } /** * Custom ArrayAdapter to display filters with our own menu item */ private static class FilterAdapter extends SortedArrayAdapter<Filter> { Context context; int layoutResourceId; public FilterAdapter(Context context, int resource, List<Filter> objects) { super(context, resource, objects); this.context = context; layoutResourceId = resource; items = objects; } @Override public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; FilterHolder holder = null; if (row == null) { LayoutInflater inflater = ((Activity) context).getLayoutInflater(); row = inflater.inflate(layoutResourceId, parent, false); holder = new FilterHolder(); holder.title = (TextView) row.findViewById(R.id.filter_item_title); holder.feeds = (TextView) row.findViewById(R.id.filter_item_feeds); row.setTag(holder); } else { holder = (FilterHolder) row.getTag(); } Filter filter = items.get(position); filter.ensureFeeds(DefaultStorageProvider.getInstance(getContext())); holder.title.setText(filter.getTitle()); holder.feeds.setText(filter.getFeeds().size() + " " + context.getString(R.string.feeds)); return row; } /** * TextViews holder */ private static class FilterHolder { TextView title; TextView feeds; } } }