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.os.Bundle; import android.support.v4.app.Fragment; import android.text.InputType; 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.listener.FallibleListener; import org.rssin.rssin.Feed; import org.rssin.rssin.Filter; import org.rssin.rssin.R; import java.io.IOException; import java.util.List; /** * @author Jos. */ public class NavigationDrawerManageFeedsFragment extends Fragment { private View rootView; /** * Returns a new instance of this fragment for the given section * number. */ public static NavigationDrawerManageFeedsFragment newInstance() { NavigationDrawerManageFeedsFragment fragment = new NavigationDrawerManageFeedsFragment(); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { container.removeAllViews(); final Bundle args = getArguments(); rootView = inflater.inflate(R.layout.fragment_feeds, container, false); final Context context = rootView.getContext(); feedsView = (ListView) rootView.findViewById(R.id.feeds_list); try { feedsList = FeedsList.getInstance(context); } catch (IOException e) { Frontend.error(context, R.string.error_load_feeds, e); } feedAdapter = new FeedAdapter(context, R.layout.item_feed, feedsList.getFeeds()); feedsView.setAdapter(feedAdapter); feedsView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { Feed feed = feedAdapter.getItem(position); DefaultStorageProvider.getInstance(context).removeFeed(feed.getStorageKey()); feedsList.getFeeds().remove(feed); feedAdapter.notifyDataSetChanged(); return true; } }); return rootView; } private FeedsList feedsList; private ListView feedsView; private FeedAdapter feedAdapter; @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.feeds_action_add) { openAddDialog(); return true; } return super.onOptionsItemSelected(item); } /** * Open dialog to add new feed * 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); final Context context = rootView.getContext(); AlertDialog.Builder alert = new AlertDialog.Builder(context); alert.setTitle(getString(R.string.feeds_activity_add_feed)); alert.setMessage(getString(R.string.feeds_activity_url)); final EditText input = new EditText(context); input.setFocusable(true); input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI); input.setMaxLines(1); 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 { Feed f = new Feed(value, DefaultStorageProvider.getInstance(context), VolleyFetcher.getInstance(context), new FallibleListener<String, Object>() { @Override public void onError(Object error) { } @Override public void onReceive(String data) { feedAdapter.notifyDataSetChanged(); } }); f.store(DefaultStorageProvider.getInstance(context)); feedsList.getFeeds().add(f); feedAdapter.notifyDataSetChanged(); } catch (Exception e) { Frontend.error(context, R.string.error_save_feeds, e); } input.setText(""); //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); } }).setNegativeButton(getResources().getString(R.string.button_cancel), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { input.setText(""); //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); } }) .setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { input.setText(""); //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); } }).create(); dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); dialog.show(); } /** * Custom ArrayAdapter to display feeds with our own menu item */ private static class FeedAdapter extends SortedArrayAdapter<Feed> { Context context; int layoutResourceId; public FeedAdapter(Context context, int resource, List<Feed> 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; FeedHolder holder = null; if (row == null) { LayoutInflater inflater = ((Activity) context).getLayoutInflater(); row = inflater.inflate(layoutResourceId, parent, false); holder = new FeedHolder(); holder.title = (TextView) row.findViewById(R.id.feed_item_title); holder.url = (TextView) row.findViewById(R.id.feed_item_url); row.setTag(holder); } else { holder = (FeedHolder) row.getTag(); } Feed feed = items.get(position); holder.title.setText(feed.getTitle()); holder.url.setText(feed.getURL().toString()); return row; } /** * TextViews holder */ private static class FeedHolder { TextView title; TextView url; } } @Override public void onAttach(Activity activity) { super.onAttach(activity); } }