Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Commit

Permalink
more progress in nearby sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
iamironrabbit committed Jun 29, 2018
1 parent ab0d15e commit c241398
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,10 @@ interface IChatSession {
* Mark this chat session as seen.
*/
void markAsSeen();

/**
* Set last message (stores the last message received)
*/
void setLastMessage (String message);
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.support.annotation.CallSuper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
Expand Down Expand Up @@ -160,9 +161,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
protected void onStart() {
super.onStart();
if (!hasPermissions(this, getRequiredPermissions())) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(getRequiredPermissions(), REQUEST_CODE_REQUIRED_PERMISSIONS);
}
ActivityCompat.requestPermissions(this,getRequiredPermissions(), REQUEST_CODE_REQUIRED_PERMISSIONS);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.android.gms.nearby.connection.ConnectionInfo;
Expand All @@ -28,6 +30,7 @@
import org.awesomeapp.messenger.tasks.AddContactAsyncTask;
import org.awesomeapp.messenger.ui.ContactListItem;
import org.awesomeapp.messenger.ui.ContactViewHolder;
import org.awesomeapp.messenger.util.GlideUtils;
import org.awesomeapp.messenger.util.SecureMediaStore;

import java.io.FileNotFoundException;
Expand Down Expand Up @@ -59,8 +62,10 @@ public class NearbyShareActivity extends ConnectionsActivity {
private String mName = null;
private File mFile = null;

private static RecyclerView mList;
private static HashMap<String,Contact> contactList = new HashMap<>();
private RecyclerView mList;
private ImageView mView;

private HashMap<String,Contact> contactList = new HashMap<>();

private State mState = State.UNKNOWN;

Expand All @@ -80,11 +85,16 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
mList.setLayoutManager(new LinearLayoutManager(this));
mList.setItemAnimator(new DefaultItemAnimator());

mView = (ImageView) findViewById(R.id.nearbyIcon);

NearbyShareListRecyclerViewAdapter adapter = new NearbyShareListRecyclerViewAdapter(this,new ArrayList<Contact>(contactList.values()));
mList.setAdapter(adapter);


mName = getIntent().getStringExtra("name");
if (TextUtils.isEmpty(mName))
{
mName = mApp.getDefaultUsername();
}

if (getIntent().getData() != null)
mFile = new File(getIntent().getData().getPath());
Expand All @@ -102,20 +112,34 @@ private void ignoreContact (Contact contact)
private void confirmContact (Contact contact)
{
Endpoint endpoint = mUserEndpoint.get(contact.getAddress().getBareAddress());
connectToEndpoint(endpoint);
contact.setSubscriptionStatus(Imps.ContactsColumns.SUBSCRIPTION_TYPE_BOTH);
if (endpoint != null) {
connectToEndpoint(endpoint);
contact.setSubscriptionStatus(Imps.ContactsColumns.SUBSCRIPTION_TYPE_BOTH);
}
else
{
contactList.remove(contact);
}

refreshList();
}

private void removeContact (String username) {
contactList.remove(username);
refreshList();
}

private void addContact (String username)
{
Contact contact = new Contact(new XmppAddress(username));
contact.setName(username);
contact.setPresence(new Presence());
contact.setSubscriptionType(Imps.ContactsColumns.SUBSCRIPTION_TYPE_FROM);
contact.setSubscriptionStatus(Imps.ContactsColumns.SUBSCRIPTION_STATUS_SUBSCRIBE_PENDING);
contactList.put(contact.getAddress().getAddress(),contact);
refreshList();
if (!contactList.containsKey(username)) {
Contact contact = new Contact(new XmppAddress(username));
contact.setName(username);
contact.setPresence(new Presence());
contact.setSubscriptionType(Imps.ContactsColumns.SUBSCRIPTION_TYPE_FROM);
contact.setSubscriptionStatus(Imps.ContactsColumns.SUBSCRIPTION_STATUS_SUBSCRIBE_PENDING);
contactList.put(contact.getAddress().getAddress(), contact);
refreshList();
}
}

private void refreshList ()
Expand Down Expand Up @@ -172,6 +196,11 @@ protected void onReceive(Endpoint endpoint, Payload payload) {
true, name,
result, System.currentTimeMillis(), type,
0, msgId, mimeType);

session.setLastMessage(result);

Uri mediaUri = Uri.parse("vfs://" + fileDownload.getAbsolutePath());
GlideUtils.loadImageFromUri(this, Uri.parse(result), mView);
}

} catch (Exception e) {
Expand Down Expand Up @@ -209,7 +238,7 @@ protected void onConnectionInitiated(Endpoint endpoint, ConnectionInfo connectio
// visually see which device they connected with.
// mConnectedColor = COLORS[connectionInfo.getAuthenticationToken().hashCode() % COLORS.length];
acceptConnection(endpoint);

addContact(endpoint.getName());
}

@Override
Expand All @@ -228,6 +257,9 @@ protected void onEndpointConnected(Endpoint endpoint) {
protected void onEndpointDisconnected(Endpoint endpoint) {

setState(State.SEARCHING);

removeContact(endpoint.getName());

}

@Override
Expand All @@ -236,6 +268,8 @@ protected void onConnectionFailed(Endpoint endpoint) {
if (getState() == State.SEARCHING) {
startDiscovering();
}

removeContact(endpoint.getName());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2769,7 +2769,6 @@ synchronized ImEntity findOrCreateParticipant(String address, boolean isGroupCha

if (participant == null) {
try {

mChatGroupManager.createChatGroupAsync(address, xmppAddress.getUser(), mUser.getName());
participant = mChatGroupManager.getChatGroup(xmppAddress);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private void init(ChatGroup group, boolean isNewSession) {

mMessageURI = Imps.Messages.getContentUriByThreadId(mContactId);
if (isNewSession)
insertOrUpdateChat("");
setLastMessage("");

for (Contact c : group.getMembers()) {
mContactStatusMap.put(c.getName(), c.getPresence().getStatus());
Expand All @@ -277,7 +277,7 @@ private void init(Contact contact, boolean isNewSession) {
mChatURI = ContentUris.withAppendedId(Imps.Chats.CONTENT_URI, mContactId);

if (isNewSession)
insertOrUpdateChat(null);
setLastMessage(null);

mMessageURI = Imps.Messages.getContentUriByThreadId(mContactId);

Expand All @@ -288,7 +288,7 @@ private void init(Contact contact, boolean isNewSession) {

public void reInit ()
{
// insertOrUpdateChat(null);
// setLastMessage(null);

}

Expand Down Expand Up @@ -410,7 +410,7 @@ public void sendMessage(String text, boolean isResend) {

if (!isResend) {
insertMessageInDb(null, text, sendTime, msg.getType(), 0, msg.getID(), null);
insertOrUpdateChat(text);
setLastMessage(text);
}

int newType = mChatSession.sendMessageAsync(msg, mEnableOmemoGroups);
Expand All @@ -434,7 +434,7 @@ private org.awesomeapp.messenger.model.Message storeMediaMessage(String mediaPat
long sendTime = System.currentTimeMillis();

insertMessageInDb(null, mediaPath, sendTime, msg.getType(), 0, msg.getID(), mimeType);
insertOrUpdateChat(mediaPath);
setLastMessage(mediaPath);

return msg;
}
Expand Down Expand Up @@ -826,7 +826,12 @@ private void copyHistoryMessages(Contact oldParticipant) {
}
}*/

Uri insertOrUpdateChat(String message) {
public void setLastMessage (String message)
{
setLastMessageForUri(message);
}

private Uri setLastMessageForUri(String message) {

ContentValues values = new ContentValues(4);

Expand Down Expand Up @@ -1223,7 +1228,7 @@ else if (displayType.equals("image"))
else
messageUri = insertMessageInDb(nickname, body, time, msg.getType(), 0, msg.getID(), null);

insertOrUpdateChat(body);
setLastMessage(body);

if (messageUri == null) {
Log.e(TAG,"error saving message to the db: " + msg.getID());
Expand Down Expand Up @@ -1659,7 +1664,7 @@ public void onTransferComplete(boolean outgoing, String offerId, String from, St

int type = isVerified ? Imps.MessageType.INCOMING_ENCRYPTED_VERIFIED : Imps.MessageType.INCOMING_ENCRYPTED;

insertOrUpdateChat(filePath);
setLastMessage(filePath);

Uri messageUri = Imps.insertMessageInDb(service.getContentResolver(),
mIsGroupChat, getId(),
Expand Down Expand Up @@ -2066,7 +2071,7 @@ public String downloadMedia (String mediaLink, String msgId, String nickname)

String result = SecureMediaStore.vfsUri(fileDownload.getAbsolutePath()).toString();

insertOrUpdateChat(result);
setLastMessage(result);

Uri messageUri = Imps.insertMessageInDb(service.getContentResolver(),
mIsGroupChat, getId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.graphics.Typeface;
import android.media.AudioManager;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.ParcelFileDescriptor;
import android.os.Parcelable;
import android.os.RemoteException;
import android.preference.PreferenceManager;
Expand Down Expand Up @@ -847,27 +849,33 @@ public void startAudioRecording ()
}
}
else {
mMediaRecorder = new MediaRecorder();

String fileName = UUID.randomUUID().toString().substring(0,8) + ".m4a";
mAudioFilePath = new File(getFilesDir(), fileName);
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (am.getMode() == AudioManager.MODE_NORMAL) {

mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mMediaRecorder = new MediaRecorder();

//maybe we can modify these in the future, or allow people to tweak them
mMediaRecorder.setAudioChannels(1);
mMediaRecorder.setAudioEncodingBitRate(22050);
mMediaRecorder.setAudioSamplingRate(64000);
mMediaRecorder.setOutputFile(mAudioFilePath.getAbsolutePath());
String fileName = UUID.randomUUID().toString().substring(0, 8) + ".m4a";
mAudioFilePath = new File(getFilesDir(), fileName);

try {
mIsAudioRecording = true;
mMediaRecorder.prepare();
mMediaRecorder.start();
} catch (Exception e) {
Log.e(ImApp.LOG_TAG, "couldn't start audio", e);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

//maybe we can modify these in the future, or allow people to tweak them
mMediaRecorder.setAudioChannels(1);
mMediaRecorder.setAudioEncodingBitRate(22050);
mMediaRecorder.setAudioSamplingRate(64000);

mMediaRecorder.setOutputFile(mAudioFilePath.getAbsolutePath());

try {
mIsAudioRecording = true;
mMediaRecorder.prepare();
mMediaRecorder.start();
} catch (Exception e) {
Log.e(ImApp.LOG_TAG, "couldn't start audio", e);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3187,6 +3187,10 @@ public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
((MessageListItem)mLastSelectedView).forwardMediaFile();
mode.finish(); // Action picked, so close the CAB
return true;
case R.id.menu_message_nearby:
((MessageListItem)mLastSelectedView).nearbyMediaFile();
mode.finish(); // Action picked, so close the CAB
return true;
case R.id.menu_message_resend:
resendMessage(((MessageListItem)mLastSelectedView).getLastMessage(),((MessageListItem)mLastSelectedView).getMimeType());
mode.finish(); // Action picked, so close the CAB
Expand Down
12 changes: 10 additions & 2 deletions app/src/main/java/org/awesomeapp/messenger/ui/GalleryActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

import org.awesomeapp.messenger.ImApp;
import org.awesomeapp.messenger.MainActivity;
import org.awesomeapp.messenger.nearby.NearbyShareActivity;
import org.awesomeapp.messenger.provider.Imps;
import org.awesomeapp.messenger.service.IChatSession;
import org.awesomeapp.messenger.tasks.AddContactAsyncTask;
Expand Down Expand Up @@ -252,16 +253,23 @@ public boolean onOptionsItemSelected(MenuItem item) {
case android.R.id.home:
finish();
return true;

case R.id.menu_message_nearby:
receiveNearby();
return true;
}
return super.onOptionsItemSelected(item);
}

private void receiveNearby ()
{
Intent shareIntent = new Intent(this, NearbyShareActivity.class);
startActivity(shareIntent);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// getMenuInflater().inflate(R.menu.menu_conversation_detail, menu);
getMenuInflater().inflate(R.menu.menu_gallery, menu);
return true;
}

Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/org/awesomeapp/messenger/ui/MessageListItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import im.zom.messenger.R;

import org.awesomeapp.messenger.ImUrlActivity;
import org.awesomeapp.messenger.nearby.NearbyShareActivity;
import org.awesomeapp.messenger.service.IChatSession;
import org.awesomeapp.messenger.ui.onboarding.OnboardingManager;
import org.awesomeapp.messenger.ui.widgets.AudioWife;
Expand Down Expand Up @@ -612,6 +613,18 @@ private void forwardMediaFile (String mimeType, Uri mediaUri)

}

public void nearbyMediaFile ()
{
if (mimeType != null && mediaUri != null) {
String resharePath = "vfs:/" + mediaUri.getPath();
Intent shareIntent = new Intent(context, NearbyShareActivity.class);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setDataAndType(Uri.parse(resharePath), mimeType);
context.startActivity(shareIntent);
}

}

public void forwardMediaFile ()
{
if (mimeType != null && mediaUri != null) {
Expand Down
Loading

0 comments on commit c241398

Please sign in to comment.