Revert "New setting: "Small screen" to avoid ellipsizing on small screens"
[f-droid:fdroidclient.git] / src / org / fdroid / fdroid / views / fragments / AppListFragment.java
1 package org.fdroid.fdroid.views.fragments;
2
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.os.Bundle;
6 import android.view.View;
7 import android.view.ViewGroup;
8 import android.widget.AdapterView;
9 import android.widget.ListView;
10
11 import android.support.v4.app.Fragment;
12
13 import org.fdroid.fdroid.*;
14 import org.fdroid.fdroid.views.AppListAdapter;
15 import org.fdroid.fdroid.views.AppListView;
16
17 abstract class AppListFragment extends Fragment implements AdapterView.OnItemClickListener, Preferences.ChangeListener {
18
19     protected FDroid parent;
20
21     protected abstract AppListAdapter getAppListAdapter();
22
23     protected abstract String getFromTitle();
24
25     @Override
26     public void onCreate(Bundle savedInstanceState) {
27         super.onCreate(savedInstanceState);
28         Preferences.get().registerCompactLayoutChangeListener(this);
29     }
30
31     @Override
32     public void onDestroy() {
33         super.onDestroy();
34         Preferences.get().unregisterCompactLayoutChangeListener(this);
35     }
36
37     @Override
38     public void onAttach(Activity activity) {
39         super.onAttach(activity);
40         try {
41             parent = (FDroid)activity;
42         } catch (ClassCastException e) {
43             // I know fragments are meant to be activity agnostic, but I can't
44             // think of a better way to share the one application list between
45             // all three app list fragments.
46             throw new RuntimeException(
47                 "AppListFragment can only be attached to FDroid activity. " +
48                 "Here it was attached to a " + activity.getClass() );
49         }
50     }
51
52     public AppListManager getAppListManager() {
53         return parent.getManager();
54     }
55
56     protected AppListView createPlainAppList() {
57         AppListView view = new AppListView(getActivity());
58         ListView list = createAppListView();
59         view.addView(
60                 list,
61                 new ViewGroup.LayoutParams(
62                         ViewGroup.LayoutParams.MATCH_PARENT,
63                         ViewGroup.LayoutParams.WRAP_CONTENT));
64         view.setAppList(list);
65         return view;
66     }
67
68     protected ListView createAppListView() {
69         ListView list = new ListView(getActivity());
70         list.setFastScrollEnabled(true);
71         list.setOnItemClickListener(this);
72         list.setAdapter(getAppListAdapter());
73         return list;
74     }
75
76     @Override
77     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
78         final DB.App app = (DB.App)getAppListAdapter().getItem(position);
79         Intent intent = new Intent(getActivity(), AppDetails.class);
80         intent.putExtra("appid", app.id);
81         intent.putExtra("from", getFromTitle());
82         startActivityForResult(intent, FDroid.REQUEST_APPDETAILS);
83     }
84
85     @Override
86     public void onPreferenceChange() {
87         getAppListAdapter().notifyDataSetChanged();
88     }
89 }