Skip to content

Commit

Permalink
Issue #SB-000 merge: Merged 'release-1.7.0' in master.
Browse files Browse the repository at this point in the history
  • Loading branch information
swayangjit committed Jun 27, 2018
2 parents c823916 + 3ca3006 commit 5512f23
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 151 deletions.
29 changes: 29 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
<data android:host="@string/deeplink_base_url" android:pathPrefix="/dial" android:scheme="https" />
<data android:host="@string/deeplink_base_url" android:pathPrefix="/dial" android:scheme="http" />

<data android:host="@string/deeplink_base_url" android:pathPrefix="/play/content" android:scheme="https" />
<data android:host="@string/deeplink_base_url" android:pathPrefix="/play/content" android:scheme="http" />

<data android:host="@string/deeplink_base_url" android:pathPrefix="/play/collection" android:scheme="https" />
<data android:host="@string/deeplink_base_url" android:pathPrefix="/play/collection" android:scheme="http" />

</intent-filter>

<intent-filter>
Expand Down Expand Up @@ -103,6 +109,29 @@
<data android:host="*" />
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />

<data android:scheme="file" />
<data android:mimeType="*/*" />
<data android:pathPattern="/*.*\\.gsa" />
<data android:pathPattern="/*.*\\..*\\.gsa" />
<data android:pathPattern="/*.*\\..*\\..*\\.gsa" />
<data android:pathPattern="/*.*\\..*\\..*\\..*\\.gsa" />
<data android:host="*" />
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="*"
android:mimeType="application/gsa"
android:scheme="content" />
</intent-filter>

</config-file>

<source-file src="src/android/org/sunbird/SplashScreen.java" target-dir="src/org/sunbird/" />
Expand Down
313 changes: 175 additions & 138 deletions src/android/org/sunbird/ImportExportUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import org.ekstep.genieservices.commons.bean.ContentImportResponse;
import org.ekstep.genieservices.commons.bean.EcarImportRequest;
import org.ekstep.genieservices.commons.bean.GenieResponse;
import org.ekstep.genieservices.commons.bean.TelemetryImportRequest;
import org.ekstep.genieservices.commons.bean.enums.ContentImportStatus;
import org.ekstep.genieservices.commons.utils.CollectionUtil;
import org.ekstep.genieservices.commons.utils.StringUtil;

import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -23,165 +25,200 @@

public final class ImportExportUtil {

/**
* Initiate the import file if Genie supported the file for side loading.
*
* @param intent
* @return
*/
public static boolean initiateImportFile(Activity activity, IImport callback, Intent intent, boolean showProgressDialog) {
Uri uri = intent.getData();

if (uri == null) {
return false;
}
private static final String EXTENSION_CONTENT = "ecar";
private static final String EXTENSION_PROFILE = "epar";
private static final String EXTENSION_TELEMETRY = "gsa";

/**
* Initiate the import file if Genie supported the file for side loading.
*
* @param intent
* @return
*/
public static boolean initiateImportFile(Activity activity, IImport callback, Intent intent, boolean showProgressDialog) {
Uri uri = intent.getData();

if (uri == null) {
return false;
}

if (intent.getScheme().equals("content")) {
return importGenieSupportedFile(activity, callback, getAttachmentFilePath(activity.getApplicationContext(), uri), true, showProgressDialog);
} else if (intent.getScheme().equals("file")) {
return importGenieSupportedFile(activity, callback, uri.getPath(), false, showProgressDialog);
} else {
return false;
}

if (intent.getScheme().equals("content")) {
return importGenieSupportedFile(activity, callback, getAttachmentFilePath(activity.getApplicationContext(), uri), true, showProgressDialog);
} else if (intent.getScheme().equals("file")) {
return importGenieSupportedFile(activity, callback, uri.getPath(), false, showProgressDialog);
} else {
return false;
}

}
private static boolean importGenieSupportedFile(Activity activity, final IImport delegate, final String filePath, final boolean isAttachment, boolean showProgressDialog) {
String extension = getFileExtension(filePath);

private static boolean importGenieSupportedFile(Activity activity, final IImport delegate, final String filePath, final boolean isAttachment, boolean showProgressDialog) {
String extension;
if (!isValidExtension(filePath)) {
return false;
}

if (filePath.lastIndexOf(".") != -1 && filePath.lastIndexOf(".") != 0) {
extension = filePath.substring(filePath.lastIndexOf(".") + 1);
} else {
extension = "";
}
if (extension.equalsIgnoreCase("ecar")) {
importContent(activity, filePath, new ImportExportUtil.IImport() {
@Override
public void onImportSuccess() {

if (isAttachment) {
File file = new File(filePath);
file.delete();
}

delegate.onImportSuccess();

}

@Override
public void onImportFailure(ContentImportStatus status) {
delegate.onImportFailure(status);
}

@Override
public void onOutDatedEcarFound() {
delegate.onOutDatedEcarFound();
}
});
}else if(extension.equalsIgnoreCase("gsa")){
TelemetryImportRequest request = new TelemetryImportRequest.Builder().fromFilePath(filePath).build();
GenieService.getAsyncService().getTelemetryService().importTelemetry(request, new IResponseHandler<Void>() {
@Override
public void onSuccess(GenieResponse<Void> genieResponse) {
if (isAttachment) {
File file = new File(filePath);
file.delete();
}

delegate.onImportSuccess();
}

@Override
public void onError(GenieResponse<Void> genieResponse) {
delegate.onImportFailure(ContentImportStatus.ALREADY_EXIST);
}
});
}

if (!extension.equalsIgnoreCase("ecar")) {
return false;
return true;
}

private static String getAttachmentFilePath(Context context, Uri uri) {

if (extension.equalsIgnoreCase("ecar")) {
importContent(activity, filePath, new ImportExportUtil.IImport() {
@Override
public void onImportSuccess() {

if (isAttachment) {
File file = new File(filePath);
file.delete();
}

delegate.onImportSuccess();
InputStream is = null;
FileOutputStream os = null;
String fullPath = null;
String name = null;

try {
Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.MediaColumns.DISPLAY_NAME}, null, null, null);
cursor.moveToFirst();
int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
if (nameIndex >= 0) {
name = cursor.getString(nameIndex);
}
fullPath = Environment.getExternalStorageDirectory() + "/" + name;
is = context.getContentResolver().openInputStream(uri);
os = new FileOutputStream(fullPath);

byte[] buffer = new byte[4096];
int count;
while ((count = is.read(buffer)) > 0) {
os.write(buffer, 0, count);
}
os.close();
is.close();
} catch (Exception e) {
if (is != null) {
try {
is.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
if (fullPath != null) {
File f = new File(fullPath);
f.delete();
}
}

@Override
public void onImportFailure(ContentImportStatus status) {
delegate.onImportFailure(status);
}
return fullPath;
}

@Override
public void onOutDatedEcarFound() {
delegate.onOutDatedEcarFound();
}
});
private static void importContent(Activity activity, String filePath, IImport iImport) {
EcarImportRequest.Builder builder = new EcarImportRequest.Builder();
builder.fromFilePath(filePath);

builder.toFolder("/storage/emulated/0/Android/data/org.sunbird.app/files");
GenieService.getAsyncService().getContentService().importEcar(builder.build(), new IResponseHandler<List<ContentImportResponse>>() {
@Override
public void onSuccess(GenieResponse<List<ContentImportResponse>> genieResponse) {

List<ContentImportResponse> contentImportResponseList = genieResponse.getResult();
if (!CollectionUtil.isNullOrEmpty(contentImportResponseList)) {
ContentImportStatus importStatus = contentImportResponseList.get(0).getStatus();
switch (importStatus) {
case NOT_COMPATIBLE:
iImport.onImportFailure(ContentImportStatus.NOT_COMPATIBLE);
break;
case CONTENT_EXPIRED:
iImport.onImportFailure(ContentImportStatus.CONTENT_EXPIRED);
break;
case ALREADY_EXIST:
iImport.onImportFailure(ContentImportStatus.ALREADY_EXIST);
break;
default:
iImport.onImportSuccess();
break;

}
} else {
iImport.onImportSuccess();
}
}

@Override
public void onError(GenieResponse<List<ContentImportResponse>> genieResponse) {
iImport.onImportFailure(ContentImportStatus.IMPORT_FAILED);
}
});
}

return true;
}

private static String getAttachmentFilePath(Context context, Uri uri) {

InputStream is = null;
FileOutputStream os = null;
String fullPath = null;
String name = null;

try {
Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.MediaColumns.DISPLAY_NAME}, null, null, null);
cursor.moveToFirst();
int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
if (nameIndex >= 0) {
name = cursor.getString(nameIndex);
}
fullPath = Environment.getExternalStorageDirectory() + "/" + name;
is = context.getContentResolver().openInputStream(uri);
os = new FileOutputStream(fullPath);

byte[] buffer = new byte[4096];
int count;
while ((count = is.read(buffer)) > 0) {
os.write(buffer, 0, count);
}
os.close();
is.close();
} catch (Exception e) {
if (is != null) {
try {
is.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (Exception e1) {
e1.printStackTrace();
public static boolean isValidExtension(String filePath) {
if (StringUtil.isNullOrEmpty(filePath)) {
return false;
}
}
if (fullPath != null) {
File f = new File(fullPath);
f.delete();
}

String fileExtension = getFileExtension(filePath);

return (fileExtension.equalsIgnoreCase(EXTENSION_CONTENT) ||
fileExtension.equalsIgnoreCase(EXTENSION_TELEMETRY) ||
fileExtension.equalsIgnoreCase(EXTENSION_PROFILE));
}

return fullPath;
}

private static void importContent(Activity activity, String filePath, IImport iImport) {
EcarImportRequest.Builder builder = new EcarImportRequest.Builder();
builder.fromFilePath(filePath);

builder.toFolder("/storage/emulated/0/Android/data/org.sunbird.app/files");
GenieService.getAsyncService().getContentService().importEcar(builder.build(), new IResponseHandler<List<ContentImportResponse>>() {
@Override
public void onSuccess(GenieResponse<List<ContentImportResponse>> genieResponse) {

List<ContentImportResponse> contentImportResponseList = genieResponse.getResult();
if (!CollectionUtil.isNullOrEmpty(contentImportResponseList)) {
ContentImportStatus importStatus = contentImportResponseList.get(0).getStatus();
switch (importStatus) {
case NOT_COMPATIBLE:
iImport.onImportFailure(ContentImportStatus.NOT_COMPATIBLE);
break;
case CONTENT_EXPIRED:
iImport.onImportFailure(ContentImportStatus.CONTENT_EXPIRED);
break;
case ALREADY_EXIST:
iImport.onImportFailure(ContentImportStatus.ALREADY_EXIST);
break;
default:
iImport.onImportSuccess();
break;

}
public static String getFileExtension(String filePath) {
if (filePath.lastIndexOf(".") != -1 && filePath.lastIndexOf(".") != 0) {
return filePath.substring(filePath.lastIndexOf(".") + 1);
} else {
iImport.onImportSuccess();
return "";
}
}

@Override
public void onError(GenieResponse<List<ContentImportResponse>> genieResponse) {
iImport.onImportFailure(ContentImportStatus.IMPORT_FAILED);
}
});
}
}

public interface IImport {
void onImportSuccess();
public interface IImport {
void onImportSuccess();

void onImportFailure(ContentImportStatus status);
void onImportFailure(ContentImportStatus status);

void onOutDatedEcarFound();
}
void onOutDatedEcarFound();
}
}
Loading

0 comments on commit 5512f23

Please sign in to comment.