-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlbumDemo.java
265 lines (241 loc) · 10.6 KB
/
AlbumDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.photos.library.sample.demos;
import static com.google.photos.library.sample.Resources.TITLE;
import static com.google.photos.library.sample.demos.FilterDemo.getOnApplicationClosedFn;
import static com.google.photos.library.sample.demos.FilterDemo.getOnBackClickedFn;
import static com.google.photos.library.sample.demos.FilterDemo.getOnItemClicked;
import static com.google.photos.library.sample.helpers.UIHelper.getFormattedText;
import com.google.api.core.ApiFuture;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.photos.library.sample.components.AppPanel;
import com.google.photos.library.sample.components.CreateAlbumToolPanel;
import com.google.photos.library.sample.components.UploadToAlbumToolPanel;
import com.google.photos.library.sample.factories.PhotosLibraryClientFactory;
import com.google.photos.library.sample.helpers.UIHelper;
import com.google.photos.library.sample.suppliers.ListAlbumsSupplier;
import com.google.photos.library.sample.suppliers.SearchMediaItemSupplier;
import com.google.photos.library.sample.views.AbstractCustomView;
import com.google.photos.library.sample.views.AlbumListView;
import com.google.photos.library.sample.views.ConnectToPhotosView;
import com.google.photos.library.sample.views.LoadingView;
import com.google.photos.library.sample.views.PhotoListView;
import com.google.photos.library.v1.PhotosLibraryClient;
import com.google.photos.library.v1.proto.BatchCreateMediaItemsRequest;
import com.google.photos.library.v1.proto.ListAlbumsRequest;
import com.google.photos.library.v1.proto.SearchMediaItemsRequest;
import com.google.photos.library.v1.upload.UploadMediaItemRequest;
import com.google.photos.library.v1.upload.UploadMediaItemResponse;
import com.google.photos.types.proto.Album;
import java.awt.FontFormatException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import javax.swing.JOptionPane;
/**
* Google Photos Library API Sample.
*
* <p>Sample app for album use cases: read, add and create.
*
* <p>This sample uses the following api methods:
*
* <ul>
* <li>batchCreateMediaItems: create media items in an album
* <li>createAlbum: create a new album
* <li>listAlbums: list albums in a library
* <li>searchMediaItems: list photos in an album
* <li>uploadMediaItem: upload a photo
* </ul>
*/
public final class AlbumDemo {
public static final String ALBUM_TITLE = "Photos album sample app";
public static final String ALBUM_SAMPLE_IMAGE_RESOURCE = "/assets/album.png";
public static final String ALBUM_INTRODUCTION =
"<html>"
+ getFormattedText(
"Google Photos Library API Sample", 14 /* fontSize */, 2 /* lineMargin */)
+ getFormattedText("Albums: read, add + create", 22 /* fontSize */, 10 /* lineMargin */)
+ getFormattedText("This sample will cover", 12 /* fontSize */, 2 /* lineMargin */)
+ getFormattedText(
" - Connecting to a Google Photos library", 12 /* fontSize */, 2 /* lineMargin */)
+ getFormattedText(
" - Requesting scopes from the Google Photos user",
12 /* fontSize */,
2 /* lineMargin */)
+ getFormattedText(" - Reading the album list", 12 /* fontSize */, 2 /* lineMargin */)
+ getFormattedText(" - Creating an album", 12 /* fontSize */, 2 /* lineMargin */)
+ getFormattedText(
" - Adding media items to an album", 12 /* fontSize */, 2 /* lineMargin */)
+ "</html>";
private static final List<String> REQUIRED_SCOPES =
ImmutableList.of(
"https://www.googleapis.com/auth/photoslibrary.readonly",
"https://www.googleapis.com/auth/photoslibrary.appendonly");
private static final String FILE_ACCESS_MODE = "r";
private AlbumDemo() {}
/**
* Runs the album sample. An optional path to a credentials file can be specified as the first
* commandline argument.
*/
public static void main(String[] args) {
// If the first argument is set, it contains the path to the credentials file.
Optional<String> credentialsFile = Optional.empty();
if (args.length > 0) {
credentialsFile = Optional.of(args[0]);
}
UIHelper.setUp();
ConnectToPhotosView connectToPhotosView =
new ConnectToPhotosView(
TITLE,
ALBUM_INTRODUCTION,
ALBUM_SAMPLE_IMAGE_RESOURCE,
credentialsFile,
getOnCredentialsSelectedFn(),
getOnApplicationClosedFn() /* onViewClosed */);
connectToPhotosView.showView();
}
private static BiConsumer<ConnectToPhotosView, String> getOnCredentialsSelectedFn() {
return (connectToPhotosView, credentialsPath) -> {
connectToPhotosView.hideView();
try {
PhotosLibraryClient client =
PhotosLibraryClientFactory.createClient(credentialsPath, REQUIRED_SCOPES);
connectToPhotosView.hideView();
showAlbums(client);
} catch (Exception e) {
JOptionPane.showMessageDialog(connectToPhotosView, e.getMessage());
}
};
}
private static void showAlbums(PhotosLibraryClient client)
throws IOException, FontFormatException {
ListAlbumsRequest request = ListAlbumsRequest.getDefaultInstance();
final ListAlbumsSupplier listAlbumsSupplier = new ListAlbumsSupplier(client, request);
AppPanel appPanel =
new AppPanel(ALBUM_TITLE, getOnApplicationClosedFn() /* onSignoutClicked */);
CreateAlbumToolPanel createAlbumToolPanel =
new CreateAlbumToolPanel(getOnCreateClickedFn(client));
AlbumListView albumListView =
new AlbumListView(
appPanel,
createAlbumToolPanel,
listAlbumsSupplier,
getOnAlbumClickedFn(client),
getOnApplicationClosedFn() /* onViewClosed */);
albumListView.showView();
}
private static BiConsumer<AbstractCustomView, String> getOnCreateClickedFn(
PhotosLibraryClient client) {
return (abstractCustomView, newAlbumTitle) -> {
try {
client.createAlbum(Album.newBuilder().setTitle(newAlbumTitle).build());
abstractCustomView.updateView();
} catch (Exception e) {
JOptionPane.showMessageDialog(abstractCustomView, e.getMessage());
abstractCustomView.showView();
}
};
}
private static BiConsumer<AlbumListView, Album> getOnAlbumClickedFn(PhotosLibraryClient client) {
return (albumListView, album) -> {
albumListView.hideView();
try {
showPhotosInAlbum(albumListView, client, album, photoListView -> albumListView.showView());
} catch (Exception e) {
JOptionPane.showMessageDialog(albumListView, e.getMessage());
albumListView.showView();
}
};
}
private static void showPhotosInAlbum(
AlbumListView albumListView,
PhotosLibraryClient client,
Album album,
Consumer<PhotoListView> onAlbumClosed)
throws IOException {
SearchMediaItemsRequest request =
SearchMediaItemsRequest.newBuilder().setAlbumId(album.getId()).build();
SearchMediaItemSupplier mediaItemSupplier = new SearchMediaItemSupplier(client, request);
AppPanel appPanel =
new AppPanel(
ALBUM_TITLE,
getOnBackClickedFn(albumListView),
getOnApplicationClosedFn() /* onSignoutClicked */);
UploadToAlbumToolPanel uploadToAlbumToolPanel =
new UploadToAlbumToolPanel(album, getOnFileSelectedFn(client, album));
PhotoListView photoListView =
new PhotoListView(
appPanel, uploadToAlbumToolPanel, mediaItemSupplier, getOnItemClicked(), onAlbumClosed);
photoListView.showView();
}
private static BiConsumer<AbstractCustomView, List<String>> getOnFileSelectedFn(
PhotosLibraryClient client, Album album) {
return (abstractCustomView, mediaItemPaths) -> {
PhotoListView photoListView = (PhotoListView) abstractCustomView;
for (String mediaItemPath : mediaItemPaths) {
try {
UploadMediaItemRequest.Builder uploadRequestBuilder = UploadMediaItemRequest.newBuilder();
uploadRequestBuilder
.setFileName(mediaItemPath)
.setDataFile(new RandomAccessFile(mediaItemPath, FILE_ACCESS_MODE));
ApiFuture<UploadMediaItemResponse> uploadResponseFuture =
client.uploadMediaItemCallable().futureCall(uploadRequestBuilder.build());
// Show loading dialog while uploading
LoadingView.getLoadingView().showView();
uploadResponseFuture.addListener(
getOnUploadFinished(client, photoListView, uploadResponseFuture, album),
MoreExecutors.directExecutor());
} catch (FileNotFoundException e) {
LoadingView.getLoadingView().hideView();
JOptionPane.showMessageDialog(photoListView, e.getMessage());
}
}
};
}
private static Runnable getOnUploadFinished(
PhotosLibraryClient client,
PhotoListView photoListView,
ApiFuture<UploadMediaItemResponse> uploadResponseFuture,
Album album) {
return () -> {
try {
UploadMediaItemResponse uploadResponse = uploadResponseFuture.get();
// Check if the upload is successful
if (uploadResponse.getUploadToken().isPresent()) {
BatchCreateMediaItemsRequest.Builder createRequestBuilder =
BatchCreateMediaItemsRequest.newBuilder();
createRequestBuilder.setAlbumId(album.getId());
createRequestBuilder
.addNewMediaItemsBuilder()
.getSimpleMediaItemBuilder()
.setUploadToken(uploadResponse.getUploadToken().get());
client.batchCreateMediaItems(createRequestBuilder.build());
// Hide loading dialog after finishing creating
LoadingView.getLoadingView().hideView();
photoListView.updateView();
}
} catch (Exception e) {
LoadingView.getLoadingView().hideView();
JOptionPane.showMessageDialog(photoListView, e.getMessage());
}
};
}
}