1.加载初始桌面布局camera_ui_root,对应 quick_switcher
2.加载quick_switcher相关布局QuickSwitcherManager setVisibility
3.加载flash相关图标布局FlashViewController,mMainHandler.sendEmptyMessage(FLASH_VIEW_INIT);
4.初始化:initFlashEntryView,flash_icon.xml
5.通过mSettingController.addViewEntry();将其加入到 quick_switcher 中。
6.点击相应图标触发 mFlashEntryListener 事件,加载布局
7.触发 mFlashChoiceViewListener 点击事件重新加载布局
host/res/layout/camera_ui_root.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/top_bar"
android:layoutDirection="ltr"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<!-- 设置图标--!>
<com.mediatek.camera.common.widget.RotateImageView
android:id="@+id/mode"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="@dimen/settings_margin_left"
android:layout_marginTop="@dimen/settings_margin_top"
android:clickable="true"
android:contentDescription="@string/accessibility_mode"
android:focusable="false"
android:scaleType="fitCenter"
android:visibility="visible" />
<!-- switch 图标--!>
<LinearLayout
android:id="@+id/quick_switcher"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_marginRight="@dimen/quick_switcher_margin_right_dp"
android:layout_marginTop="@dimen/quick_switcher_margin_top"
android:layout_toRightOf="@+id/mode"
android:gravity="center_vertical"
android:layoutDirection="rtl"
android:orientation="horizontal" />
</LinearLayout>
</RelativeLayout>
quick_switcher 加载
host/src/com/mediatek/camera/ui/CameraAppUI.java
mQuickSwitcherManager = new QuickSwitcherManager(mApp, parentView);
mQuickSwitcherManager.setVisibility(View.VISIBLE);//1111
mQuickSwitcherManager.setModeChangeListener(new OnQuickModeChangedListenerImpl());
mViewManagers.add(mQuickSwitcherManager);
host/src/com/mediatek/camera/ui/AbstractViewManager.java
protected abstract View getView();
@Override
public void setVisibility(int visibility) {
if (visibility == View.VISIBLE) {
show();
} else if (visibility == View.INVISIBLE) {
hide(View.INVISIBLE);
} else if (visibility == View.GONE) {
hide(View.GONE);
}
}
@Override
protected View getView() {
LogHelper.d(TAG,"[getView]");
RuntimeException e = new RuntimeException("print stacktrace");
Stream.of(e.getStackTrace()).forEach(System.out::println);
mQuickSwitcherLayout = (LinearLayout) mParentView.findViewById(R.id.quick_switcher);
updateQuickItems();
return mQuickSwitcherLayout;
}
feature/setting/flash/src/com/mediatek/camera/feature/setting/flash/Flash.java
@Override
public void init(IApp app,
ICameraContext cameraContext,
ISettingManager.SettingController settingController) {
super.init(app, cameraContext, settingController);
...
mFlashViewController = new FlashViewController(this, app);
...
}
feature/setting/flash/src/com/mediatek/camera/feature/setting/flash/FlashViewController.java
public FlashViewController(Flash flash, IApp app) {
mFlash = flash;
mApp = app;
mMainHandler = new MainHandler(app.getActivity().getMainLooper());
mMainHandler.sendEmptyMessage(FLASH_VIEW_INIT);
}
@Override
public void handleMessage(Message msg) {
LogHelper.d(TAG, "view handleMessage: " + msg.what);
switch (msg.what) {
case FLASH_VIEW_INIT:
mFlashEntryView = initFlashEntryView();
break;
}
}
private ImageView initFlashEntryView() {
Activity activity = mApp.getActivity();
RotateImageView view = (RotateImageView) activity.getLayoutInflater().inflate(R.layout.flash_icon, null);
view.setOnClickListener(mFlashEntryListener);
mFlashIndicatorView = (RotateImageView) activity.getLayoutInflater().inflate(
R.layout.flash_indicator, null);
return view;
}
以拍照模式为例:
common/src/com/mediatek/camera/common/mode/photo/device/PhotoDevice2Controller.java
mSettingController.addViewEntry();
common/src/com/mediatek/camera/common/setting/SettingManager.java
@Override
public void addViewEntry() {
...
List<ICameraSetting> settingsRelatedToMode = getSettingByModeType(mModeType);
for (ICameraSetting setting : settingsRelatedToMode) {
if (!access.isValid()) {
break;
}
LogHelper.d(mTag, "zzj_[addViewEntry] 111");
setting.addViewEntry();
}
...
mAppUi.registerQuickIconDone();
...
}
feature/setting/flash/src/com/mediatek/camera/feature/setting/flash/Flash.java
public void addQuickSwitchIcon() {
mMainHandler.sendEmptyMessage(FLASH_VIEW_ADD_QUICK_SWITCH);
}
public void handleMessage(Message msg) {
LogHelper.d(TAG, "view handleMessage: " + msg.what);
switch (msg.what) {
case FLASH_VIEW_INIT:
mFlashEntryView = initFlashEntryView();
break;
case FLASH_VIEW_ADD_QUICK_SWITCH:
mApp.getAppUi().addToQuickSwitcher(mFlashEntryView, FLASH_PRIORITY);
updateFlashEntryView(mFlash.getValue());
mApp.getAppUi().registerOnShutterButtonListener(mShutterListener,
FLASH_SHUTTER_PRIORITY);
break;
}
}
host/src/com/mediatek/camera/ui/CameraAppUI.java
@Override
public void addToQuickSwitcher(View view, int priority) {
mQuickSwitcherManager.addToQuickSwitcher(view, priority);
}
host/src/com/mediatek/camera/ui/QuickSwitcherManager.java
public void addToQuickSwitcher(View view, int priority) {
LogHelper.d(TAG, "[registerToQuickSwitcher] priority = " + priority);
if (mQuickItems.size() > ITEM_LIMIT) {
LogHelper.w(TAG, "already reach to limit number : " + ITEM_LIMIT);
return;
}
if (!mQuickItems.containsValue(view)) {
mQuickItems.put(priority, view);
}
}
mFlashViewController.showQuickSwitchIcon(getEntryValues().size() > 1);
feature/setting/flash/src/com/mediatek/camera/feature/setting/flash/FlashViewController.java
public void showQuickSwitchIcon(boolean isShow) {
mMainHandler.obtainMessage(FLASH_VIEW_UPDATE_QUICK_SWITCH_ICON, isShow).sendToTarget();
}
public void handleMessage(Message msg) {
LogHelper.d(TAG, "view handleMessage: " + msg.what);
switch (msg.what) {
case FLASH_VIEW_UPDATE_QUICK_SWITCH_ICON:
if ((boolean) msg.obj) {
mFlashEntryView.setVisibility(View.VISIBLE);
updateFlashEntryView(mFlash.getValue());
} else {
mFlashEntryView.setVisibility(View.GONE);
}
break;
}
}
public void updateFlashEntryView(final String value) {
LogHelper.d(TAG, "[updateFlashView] currentValue = " + mFlash.getValue());
if (FLASH_ON_VALUE.equals(value)) {
mFlashEntryView.setImageResource(R.drawable.ic_flash_status_on);
mFlashEntryView.setContentDescription(mApp.getActivity().getResources().getString(
R.string.accessibility_flash_on));
} else if (FLASH_AUTO_VALUE.equals(value)) {
mFlashEntryView.setImageResource(R.drawable.ic_flash_status_auto);
mFlashEntryView.setContentDescription(mApp.getActivity().getResources().getString(
R.string.accessibility_flash_auto));
} else {
mFlashEntryView.setImageResource(R.drawable.ic_flash_status_off);
mFlashEntryView.setContentDescription(mApp.getActivity().getResources().getString(
R.string.accessibility_flash_off));
}
// Flash indicator no need to show now,would be enable later
// updateFlashIndicator(value);
}
mAppUi.registerQuickIconDone();
host/src/com/mediatek/camera/ui/CameraAppUI.java
@Override
public void registerQuickIconDone() {
mApp.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mQuickSwitcherManager.registerQuickIconDone();
}
});
}
host/src/com/mediatek/camera/ui/QuickSwitcherManager.java
public void registerQuickIconDone() {
LogHelper.d(TAG,"[registerQuickIconDone]");
updateQuickItems();
}
//加载小图标
private void updateQuickItems() {
LogHelper.d(TAG,"[updateQuickItems]");
float density = mApp.getActivity().getResources().getDisplayMetrics().density;
int marginInPix = (int) (MARGIN_IN_DP * density);
if (mQuickSwitcherLayout != null && mQuickSwitcherLayout.getChildCount() != 0) {
mQuickSwitcherLayout.removeAllViews();
}
if (mQuickSwitcherLayout != null) {
Iterator iterator = mQuickItems.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry map = (Map.Entry) iterator.next();
View view = (View) map.getValue();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(marginInPix, 0, 0, 0);
view.setLayoutParams(params);
mQuickSwitcherLayout.addView(view);
}
updateViewOrientation();
}
}
mQuickSwitcherLayout.addView(view);
//点击事件
feature/setting/flash/src/com/mediatek/camera/feature/setting/flash/FlashViewController.java
private final View.OnClickListener mFlashEntryListener = new View.OnClickListener() {
public void onClick(View view) {
if (mFlash.getEntryValues().size() <= 1) {
return;
}
if (mFlash.getEntryValues().size() > FLASH_ENTRY_LIST_SWITCH_SIZE) {
initializeFlashChoiceView();
updateChoiceView();
mApp.getAppUi().showQuickSwitcherOption(mOptionLayout);
} else {
String value = mFlash.getEntryValues().get(FLASH_ENTRY_LIST_INDEX_0);
if (value.equals(mFlash.getValue())) {
value = mFlash.getEntryValues().get(FLASH_ENTRY_LIST_INDEX_1);
}
updateFlashEntryView(value);
// Flash indicator no need to show now,would be enable later
// updateFlashIndicator(value);
mFlash.onFlashValueChanged(value);
}
}
};
private void initializeFlashChoiceView() {
if (mFlashChoiceView == null || mOptionLayout == null) {
ViewGroup viewGroup = mApp.getAppUi().getModeRootView();
mOptionLayout = mApp.getActivity().getLayoutInflater().inflate(
R.layout.flash_option, viewGroup, false);
mFlashChoiceView = mOptionLayout.findViewById(R.id.flash_choice);
mFlashOnIcon = (ImageView) mOptionLayout.findViewById(R.id.flash_on);
mFlashOffIcon = (ImageView) mOptionLayout.findViewById(R.id.flash_off);
mFlashAutoIcon = (ImageView) mOptionLayout.findViewById(R.id.flash_auto);
mFlashOffIcon.setOnClickListener(mFlashChoiceViewListener);
mFlashOnIcon.setOnClickListener(mFlashChoiceViewListener);
mFlashAutoIcon.setOnClickListener(mFlashChoiceViewListener);
}
}
private void updateChoiceView() {
if (FLASH_ON_VALUE.equals(mFlash.getValue())) {
mFlashOnIcon.setImageResource(R.drawable.ic_flash_status_on_selected);
mFlashOffIcon.setImageResource(R.drawable.ic_flash_status_off);
mFlashAutoIcon.setImageResource(R.drawable.ic_flash_status_auto);
} else if (FLASH_OFF_VALUE.equals(mFlash.getValue())) {
mFlashOnIcon.setImageResource(R.drawable.ic_flash_status_on);
mFlashOffIcon.setImageResource(R.drawable.ic_flash_status_off_selected);
mFlashAutoIcon.setImageResource(R.drawable.ic_flash_status_auto);
} else {
mFlashOnIcon.setImageResource(R.drawable.ic_flash_status_on);
mFlashOffIcon.setImageResource(R.drawable.ic_flash_status_off);
mFlashAutoIcon.setImageResource(R.drawable.ic_flash_status_auto_selected);
}
}
host/src/com/mediatek/camera/ui/CameraAppUI.java
@Override
public void showQuickSwitcherOption(View optionView) {
mApp.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mQuickSwitcherManager.showQuickSwitcherOption(optionView);
}
});
}
host/src/com/mediatek/camera/ui/QuickSwitcherManager.java
public void showQuickSwitcherOption(View optionView) {
if (mOptionRoot.getChildCount() != 0) {
LogHelper.e(TAG, "[showQuickSwitcherOption] Already has options to be shown!");
return;
}
Animation inAnim = AnimationUtils.loadAnimation(mApp.getActivity(), R.anim.anim_top_in);
mOptionRoot.addView(optionView);
int orientation = mApp.getGSensorOrientation();
CameraUtil.rotateRotateLayoutChildView(mApp.getActivity(), mOptionRoot, orientation, true);
mOptionRoot.setVisibility(View.VISIBLE);
mOptionRoot.setClickable(true);
mOptionRoot.startAnimation(inAnim);
mTopBar.setVisibility(View.GONE);
mApp.registerOnOrientationChangeListener(mOrientationChangeListener);
}
private View.OnClickListener mFlashChoiceViewListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
String value = "";
if (mFlashAutoIcon == view) {
value = FLASH_AUTO_VALUE;
} else if (mFlashOnIcon == view) {
value = FLASH_ON_VALUE;
//*/ hct.huangfei, 20201212.add torch mode for flash.
} else if (mFlash.isTorchModeSupport()&&mFlashTorchIcon == view) {
value = FLASH_TORCH_VALUE;
//*/
} else {
value = FLASH_OFF_VALUE;
}
mApp.getAppUi().hideQuickSwitcherOption();
updateFlashEntryView(value);
// Flash indicator no need to show now,would be enable later
// updateFlashIndicator(value);
mFlash.onFlashValueChanged(value);
}
};