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
*//www.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.Method;
import android.content.Context;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
publicclass Droid22Flashlight implements Flashlight {
privatestaticfinal String TAG = "qs.droid22led";
private Object mManager;
publicint getType() {
return TYPE_DROID22;
}
publicboolean isOn(Context context) {
try {
Object manager = getManager();
if (manager != null) {
Method getFlashlightEnabledMethod = manager.getClass().getMethod("getFlashlightEnabled");
return (Boolean) getFlashlightEnabledMethod.invoke(manager);
}
} catch (Exception e) {
Log.e(TAG, "", e);
}
return false;
}
publicboolean isSupported(Context context) {
// Build.VERSION.SDK_INT == 8 && ("shadow".equals(Build.BOARD) || "droid2".equals(Build.BOARD) || "sholes".equals(Build.BOARD)) && getManager() != null;
if (Build.VERSION.SDK_INT == 8 && getManager() != null) {
setOn(true, context);
boolean on = isOn(context);
setOn(false, context);
return on;
}
return false;
}
publicvoid setOn(boolean on, Context context) {
try {
Object manager = getManager();
if (manager != null) {
Method setFlashlightEnabledMethod = manager.getClass().getMethod("setFlashlightEnabled", boolean.class);
setFlashlightEnabledMethod.invoke(manager, on);
}
} catch (Exception e) {
Log.e(TAG, "", e);
}
}
private Object getManager() {
if (mManager == null) {
try {
Class<?> managerClass = Class.forName("android.os.ServiceManager");
Method methodGetService = managerClass.getMethod("getService", String.class);
IBinder hardwareService = (IBinder) methodGetService.invoke(managerClass, "hardware");
Class<?> stubClass = Class.forName("android.os.IHardwareService$Stub");
Method asInterfaceMethod = stubClass.getMethod("asInterface", IBinder.class);
mManager = asInterfaceMethod.invoke(stubClass, hardwareService);
} catch (Exception e) {
Log.e(TAG, "", e);
}
}
return mManager;
}
}