Started download button for incoming files in chat

This commit is contained in:
Sylvain Berfini 2018-11-24 10:30:03 +01:00
parent 851b219775
commit 6d9b2ab592
5 changed files with 60 additions and 7 deletions

View file

@ -19,18 +19,21 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
package org.linphone.chat; package org.linphone.chat;
import android.Manifest;
import android.content.Context; import android.content.Context;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.text.Spanned; import android.text.Spanned;
import android.text.method.LinkMovementMethod; import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.webkit.MimeTypeMap; import android.webkit.MimeTypeMap;
import android.widget.Button;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
@ -40,12 +43,15 @@ import android.widget.TextView;
import com.google.android.flexbox.FlexboxLayout; import com.google.android.flexbox.FlexboxLayout;
import org.linphone.LinphoneActivity;
import org.linphone.R; import org.linphone.R;
import org.linphone.contacts.ContactsManager; import org.linphone.contacts.ContactsManager;
import org.linphone.contacts.LinphoneContact; import org.linphone.contacts.LinphoneContact;
import org.linphone.core.Address; import org.linphone.core.Address;
import org.linphone.core.ChatMessage; import org.linphone.core.ChatMessage;
import org.linphone.core.ChatMessageListenerStub;
import org.linphone.core.Content; import org.linphone.core.Content;
import org.linphone.mediastream.Log;
import org.linphone.utils.FileUtils; import org.linphone.utils.FileUtils;
import org.linphone.utils.LinphoneUtils; import org.linphone.utils.LinphoneUtils;
import org.linphone.views.ContactAvatar; import org.linphone.views.ContactAvatar;
@ -123,7 +129,7 @@ public class ChatMessageViewHolder extends RecyclerView.ViewHolder implements Vi
void onItemClicked(int position); void onItemClicked(int position);
} }
public void bindMessage(ChatMessage message, LinphoneContact contact) { public void bindMessage(final ChatMessage message, LinphoneContact contact) {
eventLayout.setVisibility(View.GONE); eventLayout.setVisibility(View.GONE);
securityEventLayout.setVisibility(View.GONE); securityEventLayout.setVisibility(View.GONE);
rightAnchor.setVisibility(View.VISIBLE); rightAnchor.setVisibility(View.VISIBLE);
@ -207,9 +213,10 @@ public class ChatMessageViewHolder extends RecyclerView.ViewHolder implements Vi
if (fileContents.size() > 0) { if (fileContents.size() > 0) {
pictures.setVisibility(View.VISIBLE); pictures.setVisibility(View.VISIBLE);
for (Content c : fileContents) { for (Content c : fileContents) {
View content = LayoutInflater.from(mContext).inflate(R.layout.chat_bubble_content, null, false);
if (c.isFile()) { if (c.isFile()) {
String filePath = c.getFilePath(); String filePath = c.getFilePath();
View content = LayoutInflater.from(mContext).inflate(R.layout.chat_bubble_content, null, false);
View v; View v;
if (FileUtils.isExtensionImage(filePath)) { if (FileUtils.isExtensionImage(filePath)) {
@ -227,11 +234,44 @@ public class ChatMessageViewHolder extends RecyclerView.ViewHolder implements Vi
openFile((String) v.getTag()); openFile((String) v.getTag());
} }
}); });
pictures.addView(content);
} else { } else {
//TODO: download button if incoming Button download = content.findViewById(R.id.download);
download.setVisibility(View.VISIBLE);
if (mContext.getPackageManager().checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, mContext.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
String filename = c.getName();
File file = new File(FileUtils.getStorageDirectory(mContext), filename);
int prefix = 1;
while (file.exists()) {
file = new File(FileUtils.getStorageDirectory(mContext), prefix + "_" + filename);
Log.w("File with that name already exists, renamed to " + prefix + "_" + filename);
prefix += 1;
}
download.setTag(c);
c.setFilePath(file.getPath());
download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Content c = (Content) v.getTag();
message.downloadContent(c);
}
});
message.setListener(new ChatMessageListenerStub() {
@Override
public void onMsgStateChanged(ChatMessage msg, ChatMessage.State state) {
//TODO: invalidate
}
});
} else {
Log.w("WRITE_EXTERNAL_STORAGE permission not granted, won't be able to store the downloaded file");
LinphoneActivity.instance().checkAndRequestExternalStoragePermission();
}
} }
pictures.addView(content);
} }
} }
} }

View file

@ -186,8 +186,6 @@
android:layout_above="@id/remote_composing" android:layout_above="@id/remote_composing"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_below="@+id/top"/> android:layout_below="@+id/top"/>
<ImageView <ImageView

View file

@ -135,6 +135,7 @@
android:layout_height="10dp" android:layout_height="10dp"
android:src="@drawable/imdn_received" android:src="@drawable/imdn_received"
android:layout_marginLeft="5dp" android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_alignParentRight="true" android:layout_alignParentRight="true"
android:layout_alignBottom="@id/background"/> android:layout_alignBottom="@id/background"/>
@ -145,6 +146,7 @@
android:indeterminateTint="@color/colorA" android:indeterminateTint="@color/colorA"
android:src="@drawable/imdn_received" android:src="@drawable/imdn_received"
android:layout_marginLeft="5dp" android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_alignParentRight="true" android:layout_alignParentRight="true"
android:layout_alignBottom="@id/background"/> android:layout_alignBottom="@id/background"/>

View file

@ -23,4 +23,16 @@
android:layout_height="100dp" android:layout_height="100dp"
android:layout_margin="5dp"/> android:layout_margin="5dp"/>
<Button
android:id="@+id/download"
android:visibility="gone"
android:text="@string/download_file"
android:background="@drawable/resizable_assistant_button"
style="@style/font8"
android:ellipsize="end"
android:layout_width="137dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:padding="10dp"/>
</RelativeLayout> </RelativeLayout>

View file

@ -239,6 +239,7 @@
<string name="security_level_downgraded">Security level downgraded because of %s</string> <string name="security_level_downgraded">Security level downgraded because of %s</string>
<string name="participant_max_count_exceeded">Max participant count exceeded by %s</string> <string name="participant_max_count_exceeded">Max participant count exceeded by %s</string>
<string name="unexpected_event">Unexpected event %i for %s</string> <string name="unexpected_event">Unexpected event %i for %s</string>
<string name="download_file">Download</string>
<!-- Status Bar --> <!-- Status Bar -->
<string name="status_connected">Registered</string> <string name="status_connected">Registered</string>