1/*
2 * Copyright (C) 2016 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 */
16
17package com.android.dialer.voicemail;
18
19import android.content.Intent;
20import android.database.Cursor;
21import android.os.Bundle;
22import android.support.v7.app.ActionBar;
23import android.support.v7.widget.LinearLayoutManager;
24import android.support.v7.widget.RecyclerView;
25import android.view.MenuItem;
26import android.view.View;
27
28import com.android.contacts.common.GeoUtil;
29import com.android.dialer.DialtactsActivity;
30import com.android.dialer.R;
31import com.android.dialer.TransactionSafeActivity;
32import com.android.dialer.calllog.CallLogAdapter;
33import com.android.dialer.calllog.CallLogQueryHandler;
34import com.android.dialer.calllog.ContactInfoHelper;
35import com.android.dialer.widget.EmptyContentView;
36import com.android.dialerbind.ObjectFactory;
37
38/**
39 * This activity manages all the voicemails archived by the user.
40 */
41public class VoicemailArchiveActivity extends TransactionSafeActivity
42        implements CallLogAdapter.CallFetcher, CallLogQueryHandler.Listener {
43    private RecyclerView mRecyclerView;
44    private LinearLayoutManager mLayoutManager;
45    private EmptyContentView mEmptyListView;
46    private CallLogAdapter mAdapter;
47    private VoicemailPlaybackPresenter mVoicemailPlaybackPresenter;
48    private CallLogQueryHandler mCallLogQueryHandler;
49
50    @Override
51    public boolean onOptionsItemSelected(MenuItem item) {
52        if (!isSafeToCommitTransactions()) {
53            return true;
54        }
55
56        switch (item.getItemId()) {
57            case android.R.id.home:
58                Intent intent = new Intent(this, DialtactsActivity.class);
59                // Clears any activities between VoicemailArchiveActivity and DialtactsActivity
60                // on the activity stack and reuses the existing instance of DialtactsActivity
61                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
62                startActivity(intent);
63                return true;
64        }
65        return super.onOptionsItemSelected(item);
66    }
67
68    @Override
69    protected void onCreate(Bundle savedInstanceState) {
70        super.onCreate(savedInstanceState);
71
72        setContentView(R.layout.call_log_fragment);
73
74        // Make window opaque to reduce overdraw
75        getWindow().setBackgroundDrawable(null);
76
77        ActionBar actionBar = getSupportActionBar();
78        actionBar.setDisplayShowHomeEnabled(true);
79        actionBar.setDisplayHomeAsUpEnabled(true);
80        actionBar.setDisplayShowTitleEnabled(true);
81        actionBar.setElevation(0);
82
83        mCallLogQueryHandler = new CallLogQueryHandler(this, getContentResolver(), this);
84        mVoicemailPlaybackPresenter = VoicemailArchivePlaybackPresenter
85                .getInstance(this, savedInstanceState);
86
87        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
88        mRecyclerView.setHasFixedSize(true);
89        mLayoutManager = new LinearLayoutManager(this);
90        mRecyclerView.setLayoutManager(mLayoutManager);
91        mEmptyListView = (EmptyContentView) findViewById(R.id.empty_list_view);
92        mEmptyListView.setDescription(R.string.voicemail_archive_empty);
93        mEmptyListView.setImage(R.drawable.empty_call_log);
94
95        mAdapter = ObjectFactory.newCallLogAdapter(
96                this,
97                this,
98                new ContactInfoHelper(this, GeoUtil.getCurrentCountryIso(this)),
99                mVoicemailPlaybackPresenter,
100                CallLogAdapter.ACTIVITY_TYPE_ARCHIVE);
101        mRecyclerView.setAdapter(mAdapter);
102        fetchCalls();
103    }
104
105    @Override
106    protected void onPause() {
107        mVoicemailPlaybackPresenter.onPause();
108        mAdapter.onPause();
109        super.onPause();
110    }
111
112    @Override
113    public void onResume() {
114        super.onResume();
115        mAdapter.onResume();
116        mVoicemailPlaybackPresenter.onResume();
117    }
118
119    @Override
120    public void onDestroy() {
121        mVoicemailPlaybackPresenter.onDestroy();
122        mAdapter.changeCursor(null);
123        super.onDestroy();
124    }
125
126    @Override
127    public void onSaveInstanceState(Bundle outState) {
128        super.onSaveInstanceState(outState);
129        mVoicemailPlaybackPresenter.onSaveInstanceState(outState);
130    }
131
132    @Override
133    public void fetchCalls() {
134        mCallLogQueryHandler.fetchVoicemailArchive();
135    }
136
137    @Override
138    public void onVoicemailStatusFetched(Cursor statusCursor) {
139        // Do nothing
140    }
141
142    @Override
143    public void onVoicemailUnreadCountFetched(Cursor cursor) {
144        // Do nothing
145    }
146
147    @Override
148    public void onMissedCallsUnreadCountFetched(Cursor cursor) {
149        // Do nothing
150    }
151
152    @Override
153    public boolean onCallsFetched(Cursor cursor) {
154        mAdapter.changeCursorVoicemail(cursor);
155        boolean showListView = cursor != null && cursor.getCount() > 0;
156        mRecyclerView.setVisibility(showListView ? View.VISIBLE : View.GONE);
157        mEmptyListView.setVisibility(!showListView ? View.VISIBLE : View.GONE);
158        return true;
159    }
160}
161