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.text.format.DateFormat; 23 import android.view.Menu; 24 import android.widget.ImageView; 25 import android.widget.TextView; 26 27 import androidx.annotation.NonNull; 28 import androidx.annotation.Nullable; 29 import androidx.annotation.UiThread; 30 import androidx.appcompat.app.ActionBar; 31 import androidx.appcompat.app.AppCompatActivity; 32 33 import com.android.pump.R; 34 import com.android.pump.db.MediaDb; 35 import com.android.pump.db.Other; 36 import com.android.pump.util.Globals; 37 38 import java.util.Date; 39 import java.util.concurrent.TimeUnit; 40 41 @UiThread 42 public class OtherDetailsActivity extends AppCompatActivity implements MediaDb.UpdateCallback { 43 private MediaDb mMediaDb; 44 private Other mOther; 45 start(@onNull Context context, @NonNull Other other)46 public static void start(@NonNull Context context, @NonNull Other other) { 47 Intent intent = new Intent(context, OtherDetailsActivity.class); 48 // TODO(b/123704452) Pass URI instead 49 intent.putExtra("id", other.getId()); // TODO Add constant key 50 context.startActivity(intent); 51 } 52 53 @Override onCreate(@ullable Bundle savedInstanceState)54 protected void onCreate(@Nullable Bundle savedInstanceState) { 55 super.onCreate(savedInstanceState); 56 setContentView(R.layout.activity_other_details); 57 58 setSupportActionBar(findViewById(R.id.activity_other_details_toolbar)); 59 ActionBar actionBar = getSupportActionBar(); 60 if (actionBar != null) { 61 actionBar.setDisplayShowTitleEnabled(false); 62 actionBar.setDisplayShowHomeEnabled(true); 63 actionBar.setDisplayHomeAsUpEnabled(true); 64 } 65 66 mMediaDb = Globals.getMediaDb(this); 67 mMediaDb.addOtherUpdateCallback(this); 68 69 handleIntent(); 70 } 71 72 @Override onNewIntent(@ullable Intent intent)73 protected void onNewIntent(@Nullable Intent intent) { 74 super.onNewIntent(intent); 75 setIntent(intent); 76 77 handleIntent(); 78 } 79 80 @Override onDestroy()81 protected void onDestroy() { 82 mMediaDb.removeOtherUpdateCallback(this); 83 84 super.onDestroy(); 85 } 86 87 @Override onCreateOptionsMenu(@onNull Menu menu)88 public boolean onCreateOptionsMenu(@NonNull Menu menu) { 89 getMenuInflater().inflate(R.menu.activity_pump, menu); // TODO activity_other_details ? 90 return true; 91 } 92 93 @Override onSupportNavigateUp()94 public boolean onSupportNavigateUp() { 95 // TODO It should not be necessary to override this method 96 onBackPressed(); 97 return true; 98 } 99 100 @Override onItemsInserted(int index, int count)101 public void onItemsInserted(int index, int count) { } 102 103 @Override onItemsUpdated(int index, int count)104 public void onItemsUpdated(int index, int count) { 105 for (int i = index; i < index + count; ++i) { 106 Other other = mMediaDb.getOthers().get(i); 107 if (other.equals(mOther)) { 108 updateViews(); 109 break; 110 } 111 } 112 } 113 114 @Override onItemsRemoved(int index, int count)115 public void onItemsRemoved(int index, int count) { } 116 handleIntent()117 private void handleIntent() { 118 Intent intent = getIntent(); 119 Bundle extras = intent != null ? intent.getExtras() : null; 120 if (extras != null) { 121 long id = extras.getLong("id"); 122 123 mOther = mMediaDb.getOtherById(id); 124 } else { 125 // TODO This shouldn't happen -- throw exception? 126 mOther = null; 127 } 128 129 mMediaDb.loadData(mOther); 130 updateViews(); 131 } 132 updateViews()133 private void updateViews() { 134 ImageView imageView = findViewById(R.id.activity_other_details_image); 135 TextView titleView = findViewById(R.id.activity_other_details_title); 136 TextView attributesView = findViewById(R.id.activity_other_details_attributes); 137 138 imageView.setImageURI(mOther.getThumbnailUri()); 139 titleView.setText(mOther.getTitle()); 140 141 StringBuilder attributes = new StringBuilder(); 142 if (mOther.hasDuration()) { 143 long dur = mOther.getDuration(); 144 // TODO(b/123706525) Move to string resource 145 String duration = String.format("%dm %ds", 146 TimeUnit.MILLISECONDS.toMinutes(dur), 147 TimeUnit.MILLISECONDS.toSeconds(dur) - 148 TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(dur))); 149 attributes.append(duration); 150 attributes.append('\n'); 151 } 152 if (mOther.hasDateTaken()) { 153 // TODO(b/123707011) Better formatting 154 String date = DateFormat.getLongDateFormat(this).format(new Date(mOther.getDateTaken())); 155 attributes.append(date); 156 attributes.append('\n'); 157 } 158 if (mOther.hasLatLong()) { 159 // TODO(b/123706523) Decode GPS coordinates 160 double latitude = mOther.getLatitude(); 161 double longitude = mOther.getLongitude(); 162 String latlong = String.format("%f %f", latitude, longitude); 163 attributes.append(latlong); 164 attributes.append('\n'); 165 } 166 attributesView.setText(attributes); 167 168 ImageView playView = findViewById(R.id.activity_other_details_play); 169 playView.setOnClickListener((view) -> 170 VideoPlayerActivity.start(view.getContext(), mOther)); 171 } 172 } 173