How to implement cross process lock in android? - android

I'm writing a library project for multiple APPs to use. And for some reason, I must make a function mutual exclusion for different APPs, so I need a cross-process lock. But as far as I know, in android APPs can only write to it's own file's directory in internal storage, and external storage is unreliable because some device don't have one. So file lock seems not applicable for me, so is there any other way to implement cross-process lock?
thanks~

If you do not want to (or you can not) use flock or fcntl, maybe you can use LocalServerSocket to implement a spinlock.
For example:
public class SocketLock {
public SocketLock(String name) {
mName = name;
}
public final synchronized void tryLock() throws IOException {
if (mServer == null) {
mServer = new LocalServerSocket(mName);
} else {
throw new IllegalStateException("tryLock but has locked");
}
}
public final synchronized boolean timedLock(int ms) {
long expiredTime = System.currentTimeMillis() + ms;
while (true) {
if (System.currentTimeMillis() > expiredTime) {
return false;
}
try {
try {
tryLock();
return true;
} catch (IOException e) {
// ignore the exception
}
Thread.sleep(10, 0);
} catch (InterruptedException e) {
continue;
}
}
}
public final synchronized void lock() {
while (true) {
try {
try {
tryLock();
return;
} catch (IOException e) {
// ignore the exception
}
Thread.sleep(10, 0);
} catch (InterruptedException e) {
continue;
}
}
}
public final synchronized void release() {
if (mServer != null) {
try {
mServer.close();
} catch (IOException e) {
// ignore the exception
}
}
}
private final String mName;
private LocalServerSocket mServer;
}

Related

Number picker start changing value for few seconds

I would like a numberpicker to start changing the values automaticly for few seconds .
private void changeValueByOne(final NumberPicker higherPicker, final boolean increment) {
Method method;
try {
// refelction call for
// higherPicker.changeValueByOne(true);
method = higherPicker.getClass().getDeclaredMethod("changeValueByOne", boolean.class);
method.setAccessible(true);
method.invoke(higherPicker, increment);
} catch (final NoSuchMethodException e) {
e.printStackTrace();
} catch (final IllegalArgumentException e) {
e.printStackTrace();
} catch (final IllegalAccessException e) {
e.printStackTrace();
} catch (final InvocationTargetException e) {
e.printStackTrace();
}
}
I ve implemented this method and tried
Thread thread = new Thread() {
#Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
runOnUiThread(new Runnable() {
#Override
public void run() {
changeValueByOne(wp,true);
}
});
}
};
thread.start();
But it is not ok .
I would like to have an effect as a slot machine .
Why can't you simply set the value to an incremented value? Something like this:
private void changeValueByOne(final NumberPicker higherPicker) {
int oldValue = higherPicker.getValue();
higherPicker.setValue(oldValue++);
}
EDIT
Here is the source of the method you're reflecting:
private void changeValueByOne(boolean increment) {
if (mHasSelectorWheel) {
mInputText.setVisibility(View.INVISIBLE);
if (!moveToFinalScrollerPosition(mFlingScroller)) {
moveToFinalScrollerPosition(mAdjustScroller);
}
mPreviousScrollerY = 0;
if (increment) {
mFlingScroller.startScroll(0, 0, 0, -mSelectorElementHeight, SNAP_SCROLL_DURATION);
} else {
mFlingScroller.startScroll(0, 0, 0, mSelectorElementHeight, SNAP_SCROLL_DURATION);
}
invalidate();
} else {
if (increment) {
setValueInternal(mValue + 1, true);
} else {
setValueInternal(mValue - 1, true);
}
}
}
You can see that if mHasSelectorWheel = true, it calls the startScroll method. In other words, it sounds like your NumberPicker has mHasSelectorWheel = false, in which case you're out of luck given your current implementation. It is highly advisable that you look into a custom scrollable number picker widget.

Android: How to wait IntentService for BroadcastReceiver onReceive method

I have IntentService that need to wait in method a() for results of onReceive() of BroadcastReceiver().
For now i use lmao wait(5000)... so it's not too elegant
IntentService:
private boolean methodA() {
try {
synchronized (mLocalBroadcastReceiver) {
mLocalBroadcastReceiver.wait(3000);
}
} catch (InterruptedException e) {
Log.e(TAG,"error, thread interrupted");
e.printStackTrace();
}
if(CONSTANT == true){
return true;
else
return false;
}
BroadCastRecievier:
#Override
public void onReceive(Context context, Intent intent) {
CONSTANT = true //changes somehow between true/false
}
In other words: return value of methodA depends on results of onReceive(). How to synchronize two threads?
Finally i used thread like that:
private void waitForResponse() {
//wait for response
thread = new WaitForStatus();
thread.run();
try {
synchronized (thread) {
thread.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private class WaitForAppStatus implements Runnable {
#Override
public void run() {
while (true) {
if (CONSTANT != -1) {
break;
} else {
try {
wait(400);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}

Fastest way to write byte[] chuncks on android

I want to write audio data that comes from the microphone to a file.
I have 2 threads: one "listen" for the audio data and send it to the second thread(the consumer) which stores it in a queue. The consumer thread constantly polls the queue and writes on the file the audioData as byte[].
I use RandomAccessFile for the writing. Considering that everything is syncronized in my code, should I use some non-thread-safe class like FileChannel?
below is some code snippets:
Read audio data
private void read(){
// fill the buffer with the mic input
readFully(buffer, 0, buffer.length);
// creates audioData from this buffer
WAVData audioData = new WAVData(buffer.length);
audioData.arrayCopy(buffer);
// add it to the consumer
mAudioWritter.add(audioData);
}
Write audio data - the consumer clas
public void add(WAVEntity audioEntity){
mQueue.add(audioEntity);
synchronized (mLock) {
mLock.notify();
}
}
#Override
public void run() {
synchronized (mLock) {
while(!isFinalized){
//wait if queue is empty
while(mQueue.isEmpty()){
try {
mLock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
WAVEntity entity = mQueue.poll();
if(entity != null){
try {
entity.writeOnFile(file);
} catch (AudioRecorderError e) {
// error handling
}
}
}
}
callback.threadFinished(this);
// try closing this file
try {
file.close();
} catch (IOException e) {
Log.e(getClass().getName(), e.getMessage(), e);
}
}
#Override
public boolean writeOnFile(RandomAccessFile fWriter) throws AudioRecorderError {
synchronized (data) {
//write on the file
try {
fWriter.write(data);
} catch (Exception e) {
AudioRecorderError error = new AudioRecorderError(e, "Data chunck was not written. See stack trace.");
throw error;
}
}
return false;
}

A delay of a Few seconds when trying to power on Led flash

Im trying to power on the led flash, but the led flash powers on after a delay of a few seconds.
I have a built in torch in my phone, and when I click it it the flash turns on immediately.
Whats the problem here?
Heres my code:
private void processOnClick() {
if (manuName.contains("motorola")) {
DroidLED led;
try {
led = new DroidLED();
led.enable(true);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
if (mCamera == null) {
try {
mCamera = Camera.open();
} catch (Exception e) {
e.printStackTrace();
}
}
try {
mCamera = Camera.open();
} catch (Exception e) {
e.printStackTrace();
}
if (mCamera != null) {
final Parameters params = mCamera.getParameters();
List<String> flashModes = params.getSupportedFlashModes();
if (flashModes == null) {
return;
} else {
if (count == 0) {
params.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(params);
mCamera.startPreview();
}
String flashMode = params.getFlashMode();
if (!Parameters.FLASH_MODE_TORCH.equals(flashMode)) {
if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) {
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(params);
} else {
// Toast.makeText(this,
// "Flash mode (torch) not supported",Toast.LENGTH_LONG).show();
params.setFlashMode(Parameters.FLASH_MODE_ON);
mCamera.setParameters(params);
try {
mCamera.autoFocus(new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
count = 1;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
if (mCamera == null) {
return;
}
}
private void processOffClick() {
if (manuName.contains("motorola")) {
DroidLED led;
try {
led = new DroidLED();
led.enable(false);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
}
}
}
DroidLED class:
import java.lang.reflect.Method;
import android.os.IBinder;
class DroidLED {
private Object svc = null;
private Method getFlashlightEnabled = null;
private Method setFlashlightEnabled = null;
#SuppressWarnings("unchecked")
public DroidLED() throws Exception {
try {
// call ServiceManager.getService("hardware") to get an IBinder for the service.
// this appears to be totally undocumented and not exposed in the SDK whatsoever.
Class sm = Class.forName("android.os.ServiceManager");
Object hwBinder = sm.getMethod("getService", String.class).invoke(null, "hardware");
// get the hardware service stub. this seems to just get us one step closer to the proxy
Class hwsstub = Class.forName("android.os.IHardwareService$Stub");
Method asInterface = hwsstub.getMethod("asInterface", android.os.IBinder.class);
svc = asInterface.invoke(null, (IBinder) hwBinder);
// grab the class (android.os.IHardwareService$Stub$Proxy) so we can reflect on its methods
Class proxy = svc.getClass();
// save methods
getFlashlightEnabled = proxy.getMethod("getFlashlightEnabled");
setFlashlightEnabled = proxy.getMethod("setFlashlightEnabled", boolean.class);
}
catch(Exception e) {
throw new Exception("LED could not be initialized");
}
}
public boolean isEnabled() {
try {
return getFlashlightEnabled.invoke(svc).equals(true);
}
catch(Exception e) {
return false;
}
}
public void enable(boolean tf) {
try {
setFlashlightEnabled.invoke(svc, tf);
}
catch(Exception e) {}
}
}
I took this code from some answer around stackoverflow.
Thanks for your assistance!
Do you get high latencies with the motorola?
It's just a guess, but the DroidLED constructor calls expensive system initializations.
Couldn't you do this?
public class MyWidgetClickHandler {
private DroidLED = null;
public MyWidgetClickHandler(string ManuName) {
// This is slow. It will run once at initialization.
if (ManuName != null && ManuName.toLowerCase().contains("motorola"))
DroidLED = new DroidLED();
}
public void processOnClick() {
if (DroidLED != null)
DroidLED.enable(true);
else
; // ... TODO
}
public void processOffClick() {
if (DroidLED != null)
DroidLED.enable(false);
else
; // ... TODO
}
}
There could be so much more. For example you could create a LED interface with enable and isEnabled, and have two implementations for it. One would be DroidLED and the other the CommonCameraLED.
With this it looks like this:
public class LEDFactory {
public static LED createLED(string ManuName) {
if (ManuName != null && ManuName.toLowerCase().contains("motorola"))
return new DroidLED();
else
return new CommonCameraLED();
}
}
public class MyWidgetClickHandler {
private LED myLed = null;
public MyWidgetClickHandler(string ManuName) {
myLed = LEDFactory.createLED(ManuName);
}
public void processOnClick() {
myLed.enable(true);
// NOTHING TO DO
}
public void processOffClick() {
myLed.enable(false);
// NOTHING TO DO
}
}
You could also create a Thread for initialization so that phone won't start slow.
I just came across the same problem and found a solution, but i made my tests using a Samsung Galaxy S2. This code should work on every device.
Profiling each one of the functions, i found that some calls necessary to setup the camera, sumed up to 500ms in delay, making a strobe effect impossible.
My solution was to move all those functions to a separate function i call when i want to get the camera, and reduce the "turn on" code just to the call to Camera.setParameters(). By doing this, the delay came down to only 4ms.
For example (reduced code just to to prove the point):
// First get the camera for your app (Keep this variables as class
members so the live between functions)
private void acquireCamera()
{
try
{
// Get camera
cam = Camera.open();
// This is not on your code but you should do it for compatibility
mSurfaceTexture = new SurfaceTexture(0);
cam.setPreviewTexture(mSurfaceTexture);
cam.startPreview();
camParams = cam.getParameters();
}
catch(IOException e)
{ /*...*/ }
}
// Then turn on / off as many times you want.
private void setTorch(boolean on)
{
camParams.setFlashMode(on? Camera.Parameters.FLASH_MODE_TORCH : Camera.Parameters.FLASH_MODE_OFF);
cam.setParameters(camParams);
}
// Finally release the camera when you`re done
private void releaseCamera
{
camParams = null;
cam.stopPreview();
mSurfaceTexture = null;
cam.release();
}

Crash camera after 3 shots

After I take 3 shot camera stops responding (crashes).If I try to restart application camera state == locked and default camera gets locked too.
Example code:
private Runnable SimpleShot=new Runnable()
{
#Override
public void run()
{
int i=0;
while (i<7)
{
simpleShot();
try
{
Thread.sleep(1500);
} catch (InterruptedException e)
{
e.printStackTrace();
}
i++;
}
}
};
private void simpleShot()
{
//System.gc();
camera.takePicture(shutterCallback,rawCallback, this);
}
If I don't use thread camera doesn't response after 3rd shot too. Please Help.
In end method onPictureTaken, I call camera.startPreview();
LogCat http://pastebin.com/qDcthyNe
#Override
public void onPictureTaken(byte[] paramArrayOfByte, Camera paramCamera)
{
if(Utils.isSDAval() && Utils.PrepareAppFolder())
{
String path = Utils.APP_FOLDER+"/"+"APP"+"_"+Utils.getDate()+ CameraPrefsActivity.EXT_PIC;
try
{
FileOutputStream os = new FileOutputStream(path);
os.write(paramArrayOfByte);
os.close();
} catch (Exception e)
{
Utils.ShowInfo(this,getString(R.string.app_error_io));
e.printStackTrace();
}
Utils.ShowInfo(this,getString(R.string.app_shot_ok));
SharedPreferences main_pref = PreferenceManager.getDefaultSharedPreferences(this);
String string_format = main_pref.getString("pref_preview_time","0");
int timer_val = -1;
if(string_format.contains("0"))
{
timer_val = -1;
}
if(string_format.contains("1"))
{
timer_val = 1;
}
if(string_format.contains("3"))
{
timer_val = 3;
}
if(string_format.contains("5"))
{
timer_val = 5;
}
if(string_format.contains("10"))
{
timer_val = 10;
}
if(timer_val > 0)
{
Intent intent = new Intent(this,CameraPreview.class);
CameraPreview.setupActivity(path,timer_val);
startActivity(intent);
}
}
else
{
Utils.ShowInfo(this,getString(R.string.app_error_sd));
}
paramCamera.startPreview();
}
This guy had the same problem and apparently its the emulator, try running it on a real device it worked for him.

Categories

Resources