• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.LayoutInflater;
23 import android.view.Menu;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.ImageView;
27 import android.widget.TextView;
28 
29 import androidx.annotation.NonNull;
30 import androidx.annotation.Nullable;
31 import androidx.annotation.UiThread;
32 import androidx.appcompat.app.ActionBar;
33 import androidx.appcompat.app.AppCompatActivity;
34 import androidx.recyclerview.widget.RecyclerView;
35 
36 import com.android.pump.R;
37 import com.android.pump.db.Album;
38 import com.android.pump.db.Artist;
39 import com.android.pump.db.Audio;
40 import com.android.pump.db.Genre;
41 import com.android.pump.db.MediaDb;
42 import com.android.pump.util.Globals;
43 
44 @UiThread
45 public class GenreDetailsActivity extends AppCompatActivity implements MediaDb.UpdateCallback {
46     private MediaDb mMediaDb;
47     private Genre mGenre;
48 
start(@onNull Context context, @NonNull Genre genre)49     public static void start(@NonNull Context context, @NonNull Genre genre) {
50         Intent intent = new Intent(context, GenreDetailsActivity.class);
51         // TODO(b/123704452) Pass URI instead
52         intent.putExtra("id", genre.getId()); // TODO Add constant key
53         context.startActivity(intent);
54     }
55 
56     @Override
onCreate(@ullable Bundle savedInstanceState)57     protected void onCreate(@Nullable Bundle savedInstanceState) {
58         super.onCreate(savedInstanceState);
59         setContentView(R.layout.activity_genre_details);
60 
61         setSupportActionBar(findViewById(R.id.activity_genre_details_toolbar));
62         ActionBar actionBar = getSupportActionBar();
63         if (actionBar != null) {
64             actionBar.setDisplayShowTitleEnabled(false);
65             actionBar.setDisplayShowHomeEnabled(true);
66             actionBar.setDisplayHomeAsUpEnabled(true);
67         }
68 
69         mMediaDb = Globals.getMediaDb(this);
70         mMediaDb.addGenreUpdateCallback(this);
71 
72         handleIntent();
73     }
74 
75     @Override
onNewIntent(@ullable Intent intent)76     protected void onNewIntent(@Nullable Intent intent) {
77         super.onNewIntent(intent);
78         setIntent(intent);
79 
80         handleIntent();
81     }
82 
83     @Override
onDestroy()84     protected void onDestroy() {
85         mMediaDb.removeGenreUpdateCallback(this);
86 
87         super.onDestroy();
88     }
89 
90     @Override
onCreateOptionsMenu(@onNull Menu menu)91     public boolean onCreateOptionsMenu(@NonNull Menu menu) {
92         getMenuInflater().inflate(R.menu.activity_pump, menu); // TODO activity_genre_details ?
93         return true;
94     }
95 
96     @Override
onSupportNavigateUp()97     public boolean onSupportNavigateUp() {
98         // TODO It should not be necessary to override this method
99         onBackPressed();
100         return true;
101     }
102 
103     @Override
onItemsInserted(int index, int count)104     public void onItemsInserted(int index, int count) { }
105 
106     @Override
onItemsUpdated(int index, int count)107     public void onItemsUpdated(int index, int count) {
108         for (int i = index; i < index + count; ++i) {
109             Genre genre = mMediaDb.getGenres().get(i);
110             if (genre.equals(mGenre)) {
111                 updateViews();
112                 break;
113             }
114         }
115     }
116 
117     @Override
onItemsRemoved(int index, int count)118     public void onItemsRemoved(int index, int count) { }
119 
handleIntent()120     private void handleIntent() {
121         Intent intent = getIntent();
122         Bundle extras = intent != null ? intent.getExtras() : null;
123         if (extras != null) {
124             long id = extras.getLong("id");
125 
126             mGenre = mMediaDb.getGenreById(id);
127         } else {
128             mGenre = null;
129             // TODO This shouldn't happen -- throw exception?
130         }
131 
132         mMediaDb.loadData(mGenre);
133         updateViews();
134     }
135 
updateViews()136     private void updateViews() {
137         // TODO ImageView imageView = findViewById(R.id.activity_genre_details_image);
138         TextView nameView = findViewById(R.id.activity_genre_details_name);
139         TextView countView = findViewById(R.id.activity_genre_details_count);
140 
141         // TODO imageView.setImageURI(???);
142         nameView.setText(mGenre.getName());
143         // TODO(b/123037263) I18n -- Move to resource
144         countView.setText(mGenre.getAudios().size() + " songs");
145 
146         ImageView playView = findViewById(R.id.activity_genre_details_play);
147         playView.setOnClickListener((view) ->
148                 AudioPlayerActivity.start(view.getContext(), mGenre));
149 
150         RecyclerView recyclerView = findViewById(R.id.activity_genre_details_recycler_view);
151         recyclerView.setHasFixedSize(true);
152         recyclerView.setAdapter(new GenreAdapter(mMediaDb, mGenre));
153 
154         // TODO(b/123707260) Enable view caching
155         //recyclerView.setItemViewCacheSize(0);
156         //recyclerView.setRecycledViewPool(Globals.getRecycledViewPool(requireContext()));
157     }
158 
159     private static class GenreAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
160         private final MediaDb mMediaDb;
161         private final Genre mGenre;
162 
GenreAdapter(@onNull MediaDb mediaDb, @NonNull Genre genre)163         private GenreAdapter(@NonNull MediaDb mediaDb, @NonNull Genre genre) {
164             setHasStableIds(true);
165             mMediaDb = mediaDb;
166             mGenre = genre;
167         }
168 
169         @Override
onCreateViewHolder( @onNull ViewGroup parent, int viewType)170         public @NonNull RecyclerView.ViewHolder onCreateViewHolder(
171                 @NonNull ViewGroup parent, int viewType) {
172             return new AudioViewHolder(LayoutInflater.from(parent.getContext())
173                     .inflate(viewType, parent, false));
174         }
175 
176         @Override
onBindViewHolder(@onNull RecyclerView.ViewHolder holder, int position)177         public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
178             Audio audio = mGenre.getAudios().get(position);
179             mMediaDb.loadData(audio); // TODO Where should we call this? In bind()?
180             ((AudioViewHolder) holder).bind(mGenre, audio);
181         }
182 
183         @Override
getItemCount()184         public int getItemCount() {
185             return mGenre.getAudios().size();
186         }
187 
188         @Override
getItemId(int position)189         public long getItemId(int position) {
190             return mGenre.getAudios().get(position).getId();
191         }
192 
193         @Override
getItemViewType(int position)194         public int getItemViewType(int position) {
195             return R.layout.audio;
196         }
197     }
198 
199     private static class AudioViewHolder extends RecyclerView.ViewHolder {
AudioViewHolder(@onNull View itemView)200         private AudioViewHolder(@NonNull View itemView) {
201             super(itemView);
202         }
203 
bind(@onNull Genre genre, @NonNull Audio audio)204         private void bind(@NonNull Genre genre, @NonNull Audio audio) {
205             ImageView imageView = itemView.findViewById(R.id.audio_image);
206             TextView titleView = itemView.findViewById(R.id.audio_title);
207             TextView artistView = itemView.findViewById(R.id.audio_artist);
208 
209             Album album = audio.getAlbum();
210             imageView.setImageURI(album == null ? null : album.getAlbumArtUri());
211             titleView.setText(audio.getTitle());
212             Artist artist = audio.getArtist();
213             artistView.setText(artist == null ? null : artist.getName());
214 
215             itemView.setOnClickListener((view) ->
216                     AudioPlayerActivity.start(view.getContext(), audio));
217         }
218     }
219 }
220