-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathMainActivity.java
More file actions
289 lines (248 loc) · 10.3 KB
/
MainActivity.java
File metadata and controls
289 lines (248 loc) · 10.3 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package com.snapchat.kit.creativesample;
import com.snapchat.kit.sdk.SnapCreative;
import com.snapchat.kit.sdk.creative.api.SnapCreativeKitApi;
import com.snapchat.kit.sdk.creative.exceptions.SnapMediaSizeException;
import com.snapchat.kit.sdk.creative.exceptions.SnapStickerSizeException;
import com.snapchat.kit.sdk.creative.exceptions.SnapVideoLengthException;
import com.snapchat.kit.sdk.creative.media.SnapMediaFactory;
import com.snapchat.kit.sdk.creative.media.SnapSticker;
import com.snapchat.kit.sdk.creative.models.SnapContent;
import com.snapchat.kit.sdk.creative.models.SnapLiveCameraContent;
import com.snapchat.kit.sdk.creative.models.SnapPhotoContent;
import com.snapchat.kit.sdk.creative.models.SnapVideoContent;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.VideoView;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
public enum SnapState {
NO_SNAP("Clear Snap"),
IMAGE("Select Image"),
VIDEO("Select Video");
private String mOptionText;
SnapState(String optionText) {
mOptionText = optionText;
}
public String getOptionText() {
return mOptionText;
}
public int getRequestCode() {
return ordinal();
}
}
private static final String SNAP_NAME = "snap";
private static final String STICKER_NAME = "sticker";
private SnapState mSnapState = SnapState.NO_SNAP;
private File mSnapFile;
private File mStickerFile;
private ImageView mPreviewImage;
private VideoView mPreviewVideo;
private EditText mCaptionField;
private EditText mUrlField;
private CompoundButton mSendStickerOption;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SnapCreativeKitApi snapCreativeKitApi = SnapCreative.getApi(this);
final SnapMediaFactory snapMediaFactory = SnapCreative.getMediaFactory(this);
mSnapFile = new File(getCacheDir(), SNAP_NAME);
mStickerFile = new File(getCacheDir(), STICKER_NAME);
mPreviewImage = findViewById(R.id.image_preview);
mPreviewVideo = findViewById(R.id.video_preview);
mCaptionField = findViewById(R.id.caption_field);
mUrlField = findViewById(R.id.url_field);
mSendStickerOption = findViewById(R.id.send_sticker_option);
mPreviewVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
if (!mStickerFile.exists()) {
try (InputStream inputStream = getAssets().open("sticker.png")) {
copyFile(inputStream, mStickerFile);
} catch (IOException e) {
mSendStickerOption.setEnabled(false);
Toast.makeText(this, "Failed to copy sticker asset", Toast.LENGTH_SHORT).show();
}
}
findViewById(R.id.snap_container).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openMediaSelectDialog();
}
});
findViewById(R.id.share_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
final SnapContent content;
switch (mSnapState) {
case IMAGE:
content = new SnapPhotoContent(snapMediaFactory.getSnapPhotoFromFile(mSnapFile));
break;
case VIDEO:
content = new SnapVideoContent(snapMediaFactory.getSnapVideoFromFile(mSnapFile));
break;
case NO_SNAP:
default:
content = new SnapLiveCameraContent();
}
if (!TextUtils.isEmpty(mCaptionField.getText())) {
content.setCaptionText(mCaptionField.getText().toString());
}
if (!TextUtils.isEmpty(mUrlField.getText())) {
content.setAttachmentUrl(mUrlField.getText().toString());
}
if (mSendStickerOption.isChecked()) {
final SnapSticker snapSticker = snapMediaFactory.getSnapStickerFromFile(mStickerFile);
snapSticker.setHeight(300);
snapSticker.setWidth(300);
snapSticker.setPosX(0.2f);
snapSticker.setPosY(0.8f);
snapSticker.setRotationDegreesClockwise(345.0f);
content.setSnapSticker(snapSticker);
}
snapCreativeKitApi.send(content);
} catch (SnapMediaSizeException | SnapVideoLengthException | SnapStickerSizeException e) {
Toast.makeText(view.getContext(), "Media too large to share", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onResume() {
super.onResume();
if (mSnapState == SnapState.VIDEO) {
mPreviewVideo.start();
}
}
@Override
protected void onActivityResult(
int requestCode, int resultCode, @Nullable Intent intent) {
if (requestCode == SnapState.IMAGE.getRequestCode()) {
handleImageSelect(intent);
} else if (requestCode == SnapState.VIDEO.getRequestCode()) {
handleVideoSelect(intent);
} else {
super.onActivityResult(requestCode, resultCode, intent);
}
}
private void openMediaSelectDialog() {
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.select_dialog_item);
for (SnapState state : SnapState.values()) {
adapter.add(state.getOptionText());
}
new AlertDialog.Builder(this)
.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (SnapState.values()[which]) {
case IMAGE:
selectMediaFromGallery("image/*", SnapState.IMAGE.getRequestCode());
break;
case VIDEO:
selectMediaFromGallery("video/*", SnapState.VIDEO.getRequestCode());
break;
case NO_SNAP:
reset();
}
}
})
.show();
}
private void selectMediaFromGallery(String mimeType, int resultCode) {
Intent intent = new Intent(Intent.ACTION_PICK);
if(mimeType=="video/*"){
intent.setDataAndType(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mimeType);
}
else{
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, mimeType);
}
startActivityForResult(intent, resultCode);
}
private void handleImageSelect(@Nullable Intent intent) {
if (saveContentLocally(intent)) {
reset();
try {
Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(mSnapFile));
mPreviewImage.setImageBitmap(bitmap);
mSnapState = SnapState.IMAGE;
} catch (FileNotFoundException e) {
throw new IllegalStateException("Saved the image file, but it doesn't exist!");
}
}
}
private void handleVideoSelect(@Nullable Intent intent) {
if (saveContentLocally(intent)) {
reset();
mPreviewVideo.setVideoURI(Uri.fromFile(mSnapFile));
mPreviewVideo.start();
mPreviewVideo.setVisibility(View.VISIBLE);
mSnapState = SnapState.VIDEO;
}
}
private void reset() {
mPreviewImage.setImageDrawable(null);
mPreviewVideo.setVisibility(View.GONE);
mPreviewVideo.setVideoURI(null);
mSnapState = SnapState.NO_SNAP;
}
/**
* Saves the file from the ACTION_PICK Intent locally to {@link #mSnapFile} to be accessed by our FileProvider
*/
private boolean saveContentLocally(@Nullable Intent intent) {
if (intent == null || intent.getData() == null) {
return false;
}
InputStream inputStream;
try {
inputStream = getContentResolver().openInputStream(intent.getData());
} catch (FileNotFoundException e) {
Toast.makeText(this, "Could not open file", Toast.LENGTH_SHORT).show();
return false;
}
if (inputStream == null) {
Toast.makeText(this, "File does not exist", Toast.LENGTH_SHORT).show();
return false;
}
try {
copyFile(inputStream, mSnapFile);
} catch (IOException e) {
Toast.makeText(this, "Failed save file locally", Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
private static void copyFile(InputStream inputStream, File file) throws IOException {
byte[] buffer = new byte[1024];
int length;
try (FileOutputStream outputStream = new FileOutputStream(file)) {
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
}
}
}