Merge branch 'master' of git.linphone.org:linphone-android into obiane_new

This commit is contained in:
Erwan Croze 2018-08-02 16:47:35 +02:00
commit 6bd28d8967
3 changed files with 45 additions and 184 deletions

View file

@ -22,9 +22,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
@ -37,8 +35,8 @@ import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.ParcelFileDescriptor;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
import android.telephony.TelephonyManager;
import android.text.Spanned;
import android.text.TextUtils;
@ -623,88 +621,42 @@ public final class LinphoneUtils {
}
/************************************************************************************************
* Picasa/Photos management workaround *
************************************************************************************************/
public static String getFilePath(final Context context, final Uri uri) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if ("com.android.externalstorage.documents".equals(uri.getAuthority())) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
if (split.length >= 1) return Environment.getExternalStorageDirectory() + "/" + split[1];
if (uri == null) return null;
// TODO handle non-primary volumes
}// Docs storage
else if ("com.google.android.apps.docs.storage".equals(uri.getAuthority())) {
//Google doc not supported right now
String result = null;
String name = getNameFromUri(uri, context);
try {
File localFile = createFile(context, name);
InputStream remoteFile = context.getContentResolver().openInputStream(uri);
if(copyToFile(remoteFile, localFile)) {
result = localFile.getAbsolutePath();
}
// DownloadsProvider
else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
final String id = DocumentsContract.getDocumentId(uri);
try {
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
} catch (NumberFormatException nfe) {
if (id.startsWith("raw:")) {
return id.substring(4);
}
}
}
// MediaProvider
else if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] {
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
String type = getTypeFromUri(uri, context);
String result = getDataColumn(context, uri, null, null); //
if (TextUtils.isEmpty(result))
if (uri.getAuthority().contains("com.google.android") || uri.getAuthority().contains("com.android")) {
try {
File localFile = createFile(context, null, type);
FileInputStream remoteFile = getSourceStream(context, uri);
if(copyToFile(remoteFile, localFile))
result = localFile.getAbsolutePath();
remoteFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
} else if ("file".equalsIgnoreCase(uri.getScheme())) { // File
return uri.getPath();
remoteFile.close();
} catch (IOException e) {
Log.e("Enable to get sharing file", e);
}
return null;
return result;
}
private static String getTypeFromUri(Uri uri, Context context){
ContentResolver cR = context.getContentResolver();
MimeTypeMap mime = MimeTypeMap.getSingleton();
String type = mime.getExtensionFromMimeType(cR.getType(uri));
return type;
private static String getNameFromUri(Uri uri, Context context) {
String name = null;
if (uri.getScheme().equals("content")) {
Cursor returnCursor = context.getContentResolver().query(uri, null, null, null, null);
if (returnCursor != null) {
returnCursor.moveToFirst();
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
name = returnCursor.getString(nameIndex);
returnCursor.close();
}
} else if (uri.getScheme().equals("file")) {
name = uri.getLastPathSegment();
}
return name;
}
/**
@ -738,74 +690,25 @@ public final class LinphoneUtils {
}
}
public static File createFile(Context context, String imageFileName, String type) throws IOException {
if (TextUtils.isEmpty(imageFileName))
imageFileName = getStartDate()+"."+type; // make random filename if you want.
public static File createFile(Context context, String fileName) throws IOException {
if (TextUtils.isEmpty(fileName))
fileName = getStartDate();
if (!fileName.contains(".")) {
fileName = fileName + ".unknown";
}
final File root;
imageFileName = imageFileName;
root = context.getExternalCacheDir();
if (root != null && !root.exists())
root.mkdirs();
return new File(root, imageFileName);
}
public static FileInputStream getSourceStream(Context context, Uri u) throws FileNotFoundException {
FileInputStream out = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
ParcelFileDescriptor parcelFileDescriptor =
context.getContentResolver().openFileDescriptor(u, "r");
FileDescriptor fileDescriptor = null;
if (parcelFileDescriptor != null) {
fileDescriptor = parcelFileDescriptor.getFileDescriptor();
out = new FileInputStream(fileDescriptor);
}
} else {
out = (FileInputStream) context.getContentResolver().openInputStream(u);
}
return out;
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
return new File(root, fileName);
}
public static String getRealPathFromURI(Context context, Uri contentUri) {
String[] proj = {MediaStore.Images.Media.DATA};
CursorLoader loader = new CursorLoader(context, contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
String result = cursor.getString(column_index);
@ -815,32 +718,6 @@ public final class LinphoneUtils {
return null;
}
public static String processContactUri(Context context, Uri contactUri){
ContentResolver cr = context.getContentResolver();
InputStream stream = null;
if(cr !=null) {
try {
stream = cr.openInputStream(contactUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(stream != null) {
StringBuffer fileContent = new StringBuffer("");
int ch;
try {
while ((ch = stream.read()) != -1)
fileContent.append((char) ch);
} catch (IOException e) {
e.printStackTrace();
}
String data = new String(fileContent);
return data;
}
return null;
}
return null;
}
public static String getContactNameFromVcard(String vcard){
if(vcard != null) {
String contactName = vcard.substring(vcard.indexOf("FN:") + 3);

View file

@ -132,26 +132,12 @@ public class LinphoneLauncherActivity extends Activity {
String type = intent.getType();
newIntent.setData(intent.getData());
if (Intent.ACTION_SEND.equals(action) && type != null) {
if (type.contains("text/")){
if(("text/plain").equals(type) && (String)intent.getStringExtra(Intent.EXTRA_TEXT)!= null) {
stringFileShared = intent.getStringExtra(Intent.EXTRA_TEXT);
newIntent.putExtra("msgShared", stringFileShared);
} else if(((Uri) intent.getExtras().get(Intent.EXTRA_STREAM)) != null){
stringFileShared = (LinphoneUtils.createCvsFromString(LinphoneUtils.processContactUri(getApplicationContext(), (Uri)intent.getExtras().get(Intent.EXTRA_STREAM)))).toString();
newIntent.putExtra("fileShared", stringFileShared);
}
}else {
if(((String) intent.getStringExtra(Intent.EXTRA_STREAM)) != null){
stringUriFileShared = intent.getStringExtra(Intent.EXTRA_STREAM);
}else {
fileUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
stringUriFileShared = LinphoneUtils.getRealPathFromURI(getBaseContext(), fileUri);
if(stringUriFileShared == null)
if(fileUri.getPath().contains("/0/1/mediakey:/local") || fileUri.getPath().contains("/ORIGINAL/NONE/")) {
stringUriFileShared = LinphoneUtils.getFilePath(getBaseContext(), fileUri);
}else
stringUriFileShared = fileUri.getPath();
}
if(("text/plain").equals(type) && (String)intent.getStringExtra(Intent.EXTRA_TEXT)!= null) {
stringFileShared = intent.getStringExtra(Intent.EXTRA_TEXT);
newIntent.putExtra("msgShared", stringFileShared);
} else {
fileUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
stringUriFileShared = LinphoneUtils.getFilePath(getBaseContext(), fileUri);
newIntent.putExtra("fileShared", stringUriFileShared);
}
}else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {

View file

@ -228,8 +228,6 @@ public class GroupChatFragment extends Fragment implements ChatRoomListener, Con
fileSharedUri = LinphoneUtils.getFilePath(this.getActivity().getApplicationContext(), Uri.parse(fileSharedUri));
} else if (fileSharedUri.contains("com.android.contacts/contacts/")) {
fileSharedUri = LinphoneUtils.getCVSPathFromLookupUri(fileSharedUri).toString();
} else {
fileSharedUri = LinphoneUtils.getFilePath(this.getActivity().getApplicationContext(), Uri.parse(fileSharedUri));
}
addFileToPendingList(fileSharedUri);
}