use of com.codename1.io in project CodenameOne by codenameone.
the class AndroidImplementation method createIntentForURL.
private Intent createIntentForURL(String url) {
Intent intent;
Uri uri;
try {
if (url.startsWith("intent")) {
intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
} else {
if (url.startsWith("/") || url.startsWith("file:")) {
if (!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "This is required to open the file")) {
return null;
}
}
intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
if (url.startsWith("/")) {
File f = new File(url);
Uri furi = null;
try {
furi = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".provider", f);
} catch (Exception ex) {
f = makeTempCacheCopy(f);
furi = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".provider", f);
}
if (Build.VERSION.SDK_INT < 21) {
List<ResolveInfo> resInfoList = getContext().getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
getContext().grantUriPermission(packageName, furi, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
uri = furi;
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
if (url.startsWith("file:")) {
File f = new File(removeFilePrefix(url));
System.out.println("File size: " + f.length());
Uri furi = null;
try {
furi = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".provider", f);
} catch (Exception ex) {
f = makeTempCacheCopy(f);
furi = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".provider", f);
}
if (Build.VERSION.SDK_INT < 21) {
List<ResolveInfo> resInfoList = getContext().getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
getContext().grantUriPermission(packageName, furi, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
uri = furi;
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
uri = Uri.parse(url);
}
}
String mimeType = getMimeType(url);
if (mimeType != null) {
intent.setDataAndType(uri, mimeType);
} else {
intent.setData(uri);
}
}
return intent;
} catch (Exception err) {
com.codename1.io.Log.e(err);
return null;
}
}
use of com.codename1.io in project CodenameOne by codenameone.
the class AndroidImplementation method drawShape.
@Override
public void drawShape(Object graphics, com.codename1.ui.geom.Shape shape, com.codename1.ui.Stroke stroke) {
AndroidGraphics ag = (AndroidGraphics) graphics;
Path p = cn1ShapeToAndroidPath(shape);
ag.drawPath(p, stroke);
}
use of com.codename1.io in project CodenameOne by codenameone.
the class AndroidImplementation method fixAttachmentPath.
private String fixAttachmentPath(String attachment) {
com.codename1.io.File cn1File = new com.codename1.io.File(attachment);
File mediaStorageDir = new File(new File(getContext().getCacheDir(), "intent_files"), "Attachment");
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(Display.getInstance().getProperty("AppName", "CodenameOne"), "failed to create directory");
return null;
}
}
File newFile = new File(mediaStorageDir.getPath() + File.separator + cn1File.getName());
if (newFile.exists()) {
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
newFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + "_" + cn1File.getName());
}
// Uri fileUri = Uri.fromFile(newFile);
newFile.getParentFile().mkdirs();
// Uri imageUri = Uri.fromFile(newFile);
Uri fileUri = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".provider", newFile);
try {
InputStream is = FileSystemStorage.getInstance().openInputStream(attachment);
OutputStream os = new FileOutputStream(newFile);
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) > -1) {
os.write(buf, 0, len);
}
is.close();
os.close();
} catch (IOException ex) {
Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
}
return fileUri.toString();
}
use of com.codename1.io in project CodenameOne by codenameone.
the class AndroidImplementation method createMediaRecorder.
private Media createMediaRecorder(final String path, final String mimeType, final int sampleRate, final int bitRate, final int audioChannels, final int maxDuration, final boolean redirectToAudioBuffer) throws IOException {
if (getActivity() == null) {
return null;
}
if (!checkForPermission(Manifest.permission.RECORD_AUDIO, "This is required to record audio")) {
return null;
}
final Media[] record = new Media[1];
final IOException[] error = new IOException[1];
final Object lock = new Object();
synchronized (lock) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
if (redirectToAudioBuffer) {
final int channelConfig = audioChannels == 1 ? android.media.AudioFormat.CHANNEL_IN_MONO : audioChannels == 2 ? android.media.AudioFormat.CHANNEL_IN_STEREO : android.media.AudioFormat.CHANNEL_IN_MONO;
final AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, channelConfig, AudioFormat.ENCODING_PCM_16BIT, AudioRecord.getMinBufferSize(sampleRate, channelConfig, AudioFormat.ENCODING_PCM_16BIT));
final com.codename1.media.AudioBuffer audioBuffer = com.codename1.media.MediaManager.getAudioBuffer(path, true, 64);
final boolean[] stop = new boolean[1];
record[0] = new AbstractMedia() {
private int lastTime;
private boolean isRecording;
@Override
protected void playImpl() {
if (isRecording) {
return;
}
isRecording = true;
recorder.startRecording();
fireMediaStateChange(State.Playing);
new Thread(new Runnable() {
public void run() {
float[] audioData = new float[audioBuffer.getMaxSize()];
short[] buffer = new short[AudioRecord.getMinBufferSize(recorder.getSampleRate(), recorder.getChannelCount(), AudioFormat.ENCODING_PCM_16BIT)];
int read = -1;
int index = 0;
while (isRecording && (read = recorder.read(buffer, 0, buffer.length)) >= 0) {
if (read > 0) {
for (int i = 0; i < read; i++) {
audioData[index] = ((float) buffer[i]) / 0x8000;
index++;
if (index >= audioData.length) {
audioBuffer.copyFrom(sampleRate, audioChannels, audioData, 0, index);
index = 0;
}
}
if (index > 0) {
audioBuffer.copyFrom(sampleRate, audioChannels, audioData, 0, index);
index = 0;
}
}
}
}
}).start();
}
@Override
protected void pauseImpl() {
if (!isRecording) {
return;
}
isRecording = false;
recorder.stop();
fireMediaStateChange(State.Paused);
}
@Override
public void prepare() {
}
@Override
public void cleanup() {
pauseImpl();
recorder.release();
com.codename1.media.MediaManager.releaseAudioBuffer(path);
}
@Override
public int getTime() {
if (isRecording) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
AudioTimestamp ts = new AudioTimestamp();
recorder.getTimestamp(ts, AudioTimestamp.TIMEBASE_MONOTONIC);
lastTime = (int) (ts.framePosition / ((float) sampleRate / 1000f));
}
}
return lastTime;
}
@Override
public void setTime(int time) {
}
@Override
public int getDuration() {
return getTime();
}
@Override
public void setVolume(int vol) {
}
@Override
public int getVolume() {
return 0;
}
@Override
public boolean isPlaying() {
return recorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING;
}
@Override
public Component getVideoComponent() {
return null;
}
@Override
public boolean isVideo() {
return false;
}
@Override
public boolean isFullScreen() {
return false;
}
@Override
public void setFullScreen(boolean fullScreen) {
}
@Override
public void setNativePlayerMode(boolean nativePlayer) {
}
@Override
public boolean isNativePlayerMode() {
return false;
}
@Override
public void setVariable(String key, Object value) {
}
@Override
public Object getVariable(String key) {
return null;
}
};
lock.notify();
} else {
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
if (mimeType.contains("amr")) {
recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
} else {
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.setAudioSamplingRate(sampleRate);
recorder.setAudioEncodingBitRate(bitRate);
}
if (audioChannels > 0) {
recorder.setAudioChannels(audioChannels);
}
if (maxDuration > 0) {
recorder.setMaxDuration(maxDuration);
}
recorder.setOutputFile(removeFilePrefix(path));
try {
recorder.prepare();
record[0] = new AndroidRecorder(recorder);
} catch (IllegalStateException ex) {
Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
error[0] = ex;
} finally {
lock.notify();
}
}
}
}
});
try {
lock.wait();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
if (error[0] != null) {
throw error[0];
}
return record[0];
}
}
use of com.codename1.io in project CodenameOne by codenameone.
the class GoogleImpl method onConnectionFailed.
public void onConnectionFailed(final ConnectionResult cr) {
if (AndroidNativeUtil.getActivity() == null) {
return;
}
final CodenameOneActivity main = (CodenameOneActivity) AndroidNativeUtil.getActivity();
if (!mIntentInProgress && cr.hasResolution()) {
try {
mIntentInProgress = true;
main.startIntentSenderForResult(cr.getResolution().getIntentSender(), 0, null, 0, 0, 0);
main.setIntentResultListener(new com.codename1.impl.android.IntentResultListener() {
public void onActivityResult(int requestCode, int resultCode, android.content.Intent data) {
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
main.restoreIntentResultListener();
}
});
} catch (SendIntentException e) {
// The intent was canceled before it was sent. Return to the default
// state and attempt to connect to get an updated ConnectionResult.
mIntentInProgress = false;
mGoogleApiClient.connect();
}
return;
}
if (callback != null) {
Display.getInstance().callSerially(new Runnable() {
@Override
public void run() {
callback.loginFailed(GooglePlayServicesUtil.getErrorString(cr.getErrorCode()));
}
});
}
}
Aggregations