1 /*
2  * Copyright 2018 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 
17 package com.android.pump.activity;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.view.Menu;
23 import android.widget.ImageView;
24 import android.widget.TextView;
25 
26 import androidx.annotation.NonNull;
27 import androidx.annotation.Nullable;
28 import androidx.annotation.UiThread;
29 import androidx.appcompat.app.ActionBar;
30 import androidx.appcompat.app.AppCompatActivity;
31 
32 import com.android.pump.R;
33 import com.android.pump.db.MediaDb;
34 import com.android.pump.db.Movie;
35 import com.android.pump.util.Globals;
36 
37 @UiThread
38 public class MovieDetailsActivity extends AppCompatActivity implements MediaDb.UpdateCallback {
39     private MediaDb mMediaDb;
40     private Movie mMovie;
41 
start(@onNull Context context, @NonNull Movie movie)42     public static void start(@NonNull Context context, @NonNull Movie movie) {
43         Intent intent = new Intent(context, MovieDetailsActivity.class);
44         // TODO(b/123704452) Pass URI instead
45         intent.putExtra("id", movie.getId()); // TODO Add constant key
46         context.startActivity(intent);
47     }
48 
49     @Override
onCreate(@ullable Bundle savedInstanceState)50     protected void onCreate(@Nullable Bundle savedInstanceState) {
51         super.onCreate(savedInstanceState);
52         setContentView(R.layout.activity_movie_details);
53 
54         setSupportActionBar(findViewById(R.id.activity_movie_details_toolbar));
55         ActionBar actionBar = getSupportActionBar();
56         if (actionBar != null) {
57             actionBar.setDisplayShowTitleEnabled(false);
58             actionBar.setDisplayShowHomeEnabled(true);
59             actionBar.setDisplayHomeAsUpEnabled(true);
60         }
61 
62         mMediaDb = Globals.getMediaDb(this);
63         mMediaDb.addMovieUpdateCallback(this);
64 
65         handleIntent();
66     }
67 
68     @Override
onNewIntent(@ullable Intent intent)69     protected void onNewIntent(@Nullable Intent intent) {
70         super.onNewIntent(intent);
71         setIntent(intent);
72 
73         handleIntent();
74     }
75 
76     @Override
onDestroy()77     protected void onDestroy() {
78         mMediaDb.removeMovieUpdateCallback(this);
79 
80         super.onDestroy();
81     }
82 
83     @Override
onCreateOptionsMenu(@onNull Menu menu)84     public boolean onCreateOptionsMenu(@NonNull Menu menu) {
85         getMenuInflater().inflate(R.menu.activity_pump, menu); // TODO activity_movie_details ?
86         return true;
87     }
88 
89     @Override
onSupportNavigateUp()90     public boolean onSupportNavigateUp() {
91         // TODO It should not be necessary to override this method
92         onBackPressed();
93         return true;
94     }
95 
96     @Override
onItemsInserted(int index, int count)97     public void onItemsInserted(int index, int count) { }
98 
99     @Override
onItemsUpdated(int index, int count)100     public void onItemsUpdated(int index, int count) {
101         for (int i = index; i < index + count; ++i) {
102             Movie movie = mMediaDb.getMovies().get(i);
103             if (movie.equals(mMovie)) {
104                 updateViews();
105                 break;
106             }
107         }
108     }
109 
110     @Override
onItemsRemoved(int index, int count)111     public void onItemsRemoved(int index, int count) { }
112 
handleIntent()113     private void handleIntent() {
114         Intent intent = getIntent();
115         Bundle extras = intent != null ? intent.getExtras() : null;
116         if (extras != null) {
117             long id = extras.getLong("id");
118 
119             mMovie = mMediaDb.getMovieById(id);
120         } else {
121             mMovie = null;
122             // TODO This shouldn't happen -- throw exception?
123         }
124 
125         mMediaDb.loadData(mMovie);
126         updateViews();
127     }
128 
updateViews()129     private void updateViews() {
130         ImageView imageView = findViewById(R.id.activity_movie_details_image);
131         ImageView posterView = findViewById(R.id.activity_movie_details_poster);
132         TextView titleView = findViewById(R.id.activity_movie_details_title);
133         TextView attributesView = findViewById(R.id.activity_movie_details_attributes);
134         TextView synopsisView = findViewById(R.id.activity_movie_details_synopsis);
135 
136         imageView.setImageURI(mMovie.getThumbnailUri());
137         posterView.setImageURI(mMovie.getPosterUri());
138         titleView.setText(mMovie.getTitle());
139         attributesView.setText("1h 20m"); // TODO(b/123707108) Implement
140         synopsisView.setText(getSynopsis());
141 
142         ImageView playView = findViewById(R.id.activity_movie_details_play);
143         playView.setOnClickListener((view) ->
144                 VideoPlayerActivity.start(view.getContext(), mMovie));
145     }
146 
getSynopsis()147     private String getSynopsis() {
148         return (mMovie.getSynopsis() != null) ? mMovie.getSynopsis()
149                 : mMovie.getDescription();
150     }
151 }
152