Skip to content
This repository has been archived by the owner on Jul 4, 2018. It is now read-only.

Commit

Permalink
[New] Attach images to direct messages
Browse files Browse the repository at this point in the history
  • Loading branch information
klinker24 committed Jun 7, 2017
1 parent dcf1304 commit 146f277
Show file tree
Hide file tree
Showing 8 changed files with 511 additions and 403 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
Expand Down Expand Up @@ -89,6 +90,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -769,6 +771,10 @@ private void startUcrop(Uri sourceUri) {
}
}

public static boolean isAndroidN() {
return Build.VERSION.SDK_INT > Build.VERSION_CODES.M || Build.VERSION.CODENAME.equals("N");
}

public static final int SELECT_PHOTO = 100;
public static final int CAPTURE_IMAGE = 101;
public static final int SELECT_GIF = 102;
Expand Down Expand Up @@ -960,29 +966,25 @@ protected Boolean doInBackground(String... args) {
try {
Twitter twitter = Utils.getTwitter(getApplicationContext(), settings);

String sendTo = contactEntry.getText().toString().replace("@", "").replace(" ", "");
User user = twitter.showUser(sendTo);
MessageData data = new MessageData(user.getId(), status);

if (!attachedUri.equals("")) {
try {
for (int i = 0; i < imagesAttached; i++) {
File outputDir = context.getCacheDir();
File f = File.createTempFile("compose", "picture_" + i, outputDir);

Bitmap bitmap = getBitmapToSend(Uri.parse(attachedUri[i]));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
byte[] bitmapdata = bos.toByteArray();

FileOutputStream fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();

// we wont attach any text to this image at least, since it is a direct message
TwitPicHelper helper = new TwitPicHelper(twitter, " ", f, context);
String url = helper.uploadForUrl();
File f;

status += " " + url;
if (attachmentType == null) {
// image file
f = ImageUtils.scaleToSend(context, Uri.parse(attachedUri[0]));
} else {
f = new File(URI.create(attachedUri[0]));
}

UploadedMedia media = twitter.uploadMedia(f);
data.setMediaId(media.getMediaId());
} catch (Exception e) {
e.printStackTrace();
runOnUiThread(new Runnable() {
@Override
public void run() {
Expand All @@ -993,11 +995,8 @@ public void run() {

}

String sendTo = contactEntry.getText().toString().replace("@", "").replace(" ", "");

twitter.sendDirectMessage(sendTo, status);

return true;
DirectMessageEvent event = twitter.createMessage(data);
return event != null;

} catch (TwitterException e) {
e.printStackTrace();
Expand Down Expand Up @@ -1468,7 +1467,7 @@ public Bitmap decodeSampledBitmapFromResourceMemOpt(
}
}

public int calculateInSampleSize(BitmapFactory.Options opt, int reqWidth, int reqHeight) {
public static int calculateInSampleSize(BitmapFactory.Options opt, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = opt.outHeight;
final int width = opt.outWidth;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
Expand All @@ -35,7 +37,9 @@
import android.widget.ListPopupWindow;
import android.widget.Toast;

import com.klinker.android.twitter.BuildConfig;
import com.klinker.android.twitter.R;
import com.klinker.android.twitter.activities.GiphySearch;
import com.klinker.android.twitter.adapters.AutoCompletePeopleAdapter;
import com.klinker.android.twitter.data.sq_lite.FollowersDataSource;
import com.klinker.android.twitter.utils.UserAutoCompleteHelper;
Expand Down Expand Up @@ -79,37 +83,7 @@ public void setUpLayout() {
attachButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (imagesAttached > 0 && !sharedPrefs.getBoolean("know_twitpic_for_mult_attach", false)) {
new AlertDialog.Builder(context)
.setTitle(context.getResources().getString(R.string.twitpic_disclaimer))
.setMessage(getResources().getString(R.string.twitpic_disclaimer_multi_summary))
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
attachImage();
dialogInterface.dismiss();
}
})
.setNeutralButton(R.string.dont_show_again, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
sharedPrefs.edit().putBoolean("know_twitpic_for_mult_attach", true).commit();
attachImage();
dialogInterface.dismiss();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.create()
.show();
} else {
attachImage();
}

attachImage();
}
});

Expand Down Expand Up @@ -231,7 +205,7 @@ private void sendStatus(String status) {

public void attachImage() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setItems(R.array.attach_options, new DialogInterface.OnClickListener() {
builder.setItems(R.array.attach_dm_options, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if(item == 0) { // take picture
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Expand All @@ -246,35 +220,50 @@ public void onClick(DialogInterface dialog, int item) {
}
}

captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(captureIntent, CAPTURE_IMAGE);
} else { // attach picture
if (attachedUri == null || attachedUri.equals("")) {
Intent photoPickerIntent = new Intent();
photoPickerIntent.setType("image/*");
photoPickerIntent.setAction(Intent.ACTION_GET_CONTENT);
try {
startActivityForResult(Intent.createChooser(photoPickerIntent,
"Select Picture"), SELECT_PHOTO);
} catch (Throwable t) {
// no app to preform this..? hmm, tell them that I guess
Toast.makeText(context, "No app available to select pictures!", Toast.LENGTH_SHORT).show();
captureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

try {
Uri photoURI = FileProvider.getUriForFile(context,
BuildConfig.APPLICATION_ID + ".provider", f);

captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(captureIntent, CAPTURE_IMAGE);
} catch (Exception e) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Toast.makeText(ComposeDMActivity.this, "Have you given Talon the storage permission?", Toast.LENGTH_LONG).show();
}
} else {
attachedUri = "";
attachImage.setImageDrawable(null);
attachImage.setVisibility(View.GONE);
Intent photoPickerIntent = new Intent();
}

} else if (item == 1) { // attach picture
try {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PHOTO);
} catch (Exception e) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
photoPickerIntent.setAction(Intent.ACTION_GET_CONTENT);
try {
startActivityForResult(Intent.createChooser(photoPickerIntent,
"Select Picture"), SELECT_PHOTO);
} catch (Throwable t) {
// no app to preform this..? hmm, tell them that I guess
Toast.makeText(context, "No app available to select pictures!", Toast.LENGTH_SHORT).show();
}
startActivityForResult(Intent.createChooser(photoPickerIntent,
"Select Picture"), SELECT_PHOTO);
}
} else if (item == 2) {
Toast.makeText(ComposeDMActivity.this, "GIFs must be less than 5 MB", Toast.LENGTH_SHORT).show();

try {
Intent gifIntent = new Intent();
gifIntent.setType("image/gif");
gifIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(gifIntent, SELECT_GIF);
} catch (Exception e) {
Intent gifIntent = new Intent();
gifIntent.setType("image/gif");
gifIntent.setAction(Intent.ACTION_PICK);
startActivityForResult(gifIntent, SELECT_GIF);
}
} else if (item == 3) {
Intent gif = new Intent(context, GiphySearch.class);
startActivityForResult(gif, FIND_GIF);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@

import com.klinker.android.twitter.data.sq_lite.HomeSQLiteHelper;

/**
* Created by luke on 1/20/14.
*/
public class DMCursorAdapter extends TimeLineCursorAdapter {
public DMCursorAdapter(Context context, Cursor cursor, boolean isDM) {
super(context, cursor, isDM);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.klinker.android.twitter.settings.AppSettings;
import com.klinker.android.twitter.utils.TweetLinkUtils;

import twitter4j.DirectMessage;
import twitter4j.DirectMessageEvent;
import twitter4j.MediaEntity;
import twitter4j.URLEntity;
import twitter4j.User;

public class DMDataSource {

Expand Down Expand Up @@ -129,6 +132,51 @@ public synchronized void createDirectMessage(DirectMessage status, int account)

}

public synchronized void createSentDirectMessage(DirectMessageEvent status, User recipient, AppSettings settings, int account) {
ContentValues values = new ContentValues();
long time = status.getCreatedTimestamp().getTime();

String[] html = TweetLinkUtils.getLinksInStatus(status);
String text = html[0];
String media = html[1];
String url = html[2];
String hashtags = html[3];
String users = html[4];

values.put(DMSQLiteHelper.COLUMN_ACCOUNT, account);
values.put(DMSQLiteHelper.COLUMN_TEXT, text);
values.put(DMSQLiteHelper.COLUMN_TWEET_ID, status.getId());
values.put(DMSQLiteHelper.COLUMN_NAME, settings.myName);
values.put(DMSQLiteHelper.COLUMN_PRO_PIC, settings.myProfilePicUrl);
values.put(DMSQLiteHelper.COLUMN_SCREEN_NAME, settings.myScreenName);
values.put(DMSQLiteHelper.COLUMN_TIME, time);
values.put(DMSQLiteHelper.COLUMN_RETWEETER, recipient.getScreenName());
values.put(DMSQLiteHelper.COLUMN_EXTRA_ONE, recipient.getOriginalProfileImageURL());
values.put(DMSQLiteHelper.COLUMN_EXTRA_TWO, recipient.getName());
values.put(HomeSQLiteHelper.COLUMN_PIC_URL, media);
values.put(DMSQLiteHelper.COLUMN_EXTRA_THREE, TweetLinkUtils.getGIFUrl(status.getMediaEntities(), url));

MediaEntity[] entities = status.getMediaEntities();

if (entities.length > 0) {
values.put(DMSQLiteHelper.COLUMN_PIC_URL, entities[0].getMediaURL());
}

URLEntity[] urls = status.getUrlEntities();
for (URLEntity u : urls) {
Log.v("inserting_dm", "url here: " + u.getExpandedURL());
values.put(DMSQLiteHelper.COLUMN_URL, u.getExpandedURL());
}

try {
database.insert(DMSQLiteHelper.TABLE_DM, null, values);
} catch (Exception e) {
open();
database.insert(DMSQLiteHelper.TABLE_DM, null, values);
}

}

public synchronized void deleteTweet(long tweetId) {
long id = tweetId;

Expand Down
Loading

0 comments on commit 146f277

Please sign in to comment.