If you think the Android project camp-food-manager listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright (C) 2010 Sergej Shafarenka, beworx.com
*/*fromwww.java2s.com*/
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/package com.V4Creations.FSMK.campfoodmanager.flash;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import android.content.Context;
import android.os.Vibrator;
import android.util.Log;
publicclass Moto21LedFlashlight implements Flashlight {
privatestaticfinal String TAG = "qs.motoled";
private Object mService;
publicboolean isOn(Context context) {
try {
Object service = getFlashlightService(context);
Method getFlashlightEnabled = service.getClass().getMethod("getFlashlightEnabled");
Boolean res = (Boolean) getFlashlightEnabled.invoke(service);
return res.booleanValue();
} catch (Exception e) {
Log.e(TAG, "cannot get flashlight state", e);
return false;
}
}
publicvoid setOn(boolean on, Context context) {
try {
Object service = getFlashlightService(context);
Method setFlashlightEnabled = service.getClass().getMethod("setFlashlightEnabled", boolean.class);
setFlashlightEnabled.invoke(service, on);
} catch (Exception e) {
Log.e(TAG, "cannot enable flashlight", e);
}
}
private Object getFlashlightService(Context context) throws Exception {
Object service = mService;
if (service == null) {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
Field f = Class.forName(vibrator.getClass().getName()).getDeclaredField("mService");
f.setAccessible(true);
service = mService = f.get(vibrator);
}
return mService;
}
publicboolean isSupported(Context context) {
setOn(true, context);
boolean supported = isOn(context);
setOn(false, context);
return supported;
}
publicint getType() {
return TYPE_MOTO21;
}
}