1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.messaging.ui;
17
18import android.app.Fragment;
19import android.content.Context;
20import android.content.Intent;
21import android.database.Cursor;
22import android.os.Bundle;
23import android.support.v4.widget.CursorAdapter;
24import android.view.LayoutInflater;
25import android.view.View;
26import android.view.ViewGroup;
27import android.widget.ListView;
28
29import com.android.messaging.R;
30import com.android.messaging.datamodel.binding.Binding;
31import com.android.messaging.datamodel.binding.BindingBase;
32import com.android.messaging.datamodel.data.BlockedParticipantsData;
33import com.android.messaging.datamodel.data.BlockedParticipantsData.BlockedParticipantsDataListener;
34import com.android.messaging.datamodel.DataModel;
35import com.android.messaging.util.Assert;
36
37/**
38 * Show a list of currently blocked participants.
39 */
40public class BlockedParticipantsFragment extends Fragment
41        implements BlockedParticipantsDataListener {
42    private ListView mListView;
43    private BlockedParticipantListAdapter mAdapter;
44    private final Binding<BlockedParticipantsData> mBinding =
45            BindingBase.createBinding(this);
46
47    @Override
48    public void onCreate(final Bundle savedInstanceState) {
49        super.onCreate(savedInstanceState);
50    }
51
52    @Override
53    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
54            final Bundle savedInstanceState) {
55        final View view =
56                inflater.inflate(R.layout.blocked_participants_fragment, container, false);
57        mListView = (ListView) view.findViewById(android.R.id.list);
58        mAdapter = new BlockedParticipantListAdapter(getActivity(), null);
59        mListView.setAdapter(mAdapter);
60        mBinding.bind(DataModel.get().createBlockedParticipantsData(getActivity(), this));
61        mBinding.getData().init(getLoaderManager(), mBinding);
62        return view;
63    }
64
65    @Override
66    public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
67        super.onActivityResult(requestCode, resultCode, data);
68    }
69
70    @Override
71    public void onDestroy() {
72        super.onDestroy();
73        mBinding.unbind();
74    }
75
76    /**
77     * An adapter to display ParticipantListItemView based on ParticipantData.
78     */
79    private class BlockedParticipantListAdapter extends CursorAdapter {
80        public BlockedParticipantListAdapter(final Context context, final Cursor cursor) {
81            super(context, cursor, 0);
82        }
83
84        @Override
85        public View newView(Context context, Cursor cursor, ViewGroup parent) {
86            return LayoutInflater.from(context)
87                    .inflate(R.layout.blocked_participant_list_item_view, parent, false);
88        }
89
90        @Override
91        public void bindView(View view, Context context, Cursor cursor) {
92            Assert.isTrue(view instanceof BlockedParticipantListItemView);
93            ((BlockedParticipantListItemView) view).bind(
94                    mBinding.getData().createParticipantListItemData(cursor));
95        }
96    }
97
98    @Override
99    public void onBlockedParticipantsCursorUpdated(Cursor cursor) {
100        mAdapter.swapCursor(cursor);
101    }
102}
103