snapcam gps定位图标以及与系统设置定位开关同步问题
程序员文章站
2022-05-08 22:36:15
...
高通平台原生相机应用SnapdragonCamera,有菜单项“存储位置信息”,开启后拍照会在照片的详细信息中显示地点:(经度、纬度),但是定位需要一定的时间,所以无法判断拍照时是否已经定位成功。
为了能够有良好的体验,故在预览界面添加定位图标,当定位成功时,显示实心;当定位未成功时,显示空心。
另外,由于代码架构中没有与设置定位开关同步,所以当相机开启位置信息时,无法联动系统的定位开关。
一、添加定位图标
定位图标及其逻辑代码:
Index: packages/apps/SnapdragonCamera/src/com/android/camera/PhotoModule.java
===================================================================
--- packages/apps/SnapdragonCamera/src/com/android/camera/PhotoModule.java (版本 577)
+++ packages/apps/SnapdragonCamera/src/com/android/camera/PhotoModule.java (版本 579)
@@ -56,6 +56,7 @@
import android.view.SurfaceHolder;
import android.view.View;
import android.view.WindowManager;
+import android.widget.ImageView; // @ +
import android.widget.Toast;
import android.widget.ProgressBar;
import android.widget.SeekBar;
@@ -124,6 +125,8 @@
private static final String TAG = "CAM_PhotoModule";
+ private ImageView mGpsIndicator; // @ +
+
//QCom data members
public static boolean mBrightnessVisible = false;
private static final int MAX_SHARPNESS_LEVEL = 6;
@@ -626,6 +629,7 @@
brightnessProgressBar.setVisibility(View.INVISIBLE);
Storage.setSaveSDCard(
mPreferences.getString(CameraSettings.KEY_CAMERA_SAVEPATH, "0").equals("1"));
+ mGpsIndicator = (ImageView) mRootView.findViewById(R.id.onscreen_gps_indicator); // @ +
}
public CameraActivity getMainActivity() {
@@ -5209,6 +5213,33 @@
enableRecordingLocation(false);
}
+ // @ + {
+ @Override
+ public void showGpsOnScreenIndicator(boolean hasSignal) {
+ Log.d(TAG, "showGpsOnScreenIndicator hasSignal = " + hasSignal);
+ if (mGpsIndicator == null) {
+ return;
+ }
+ if (hasSignal) {
+ mGpsIndicator
+ .setImageResource(R.drawable.ic_viewfinder_gps_on);
+ } else {
+ mGpsIndicator
+ .setImageResource(R.drawable.ic_viewfinder_gps_no_signal);
+ }
+ mGpsIndicator.setVisibility(View.VISIBLE);
+ }
+
+ @Override
+ public void hideGpsOnScreenIndicator() {
+ Log.d(TAG, "hideGpsOnScreenIndicator - E");
+ if (mGpsIndicator == null) {
+ return;
+ }
+ mGpsIndicator.setVisibility(View.GONE);
+ }
+ // @ + }
+
}
Index: packages/apps/SnapdragonCamera/src/com/android/camera/VideoModule.java
===================================================================
--- packages/apps/SnapdragonCamera/src/com/android/camera/VideoModule.java (版本 577)
+++ packages/apps/SnapdragonCamera/src/com/android/camera/VideoModule.java (版本 579)
@@ -56,6 +56,7 @@
import android.view.SurfaceHolder;
import android.view.View;
import android.view.WindowManager;
+import android.widget.ImageView; // @ +
import android.widget.Toast;
import android.media.EncoderCapabilities;
import android.media.EncoderCapabilities.VideoEncoderCap;
@@ -98,6 +99,8 @@
private static final String TAG = "CAM_VideoModule";
+ private ImageView mGpsIndicator; // @ +
+
private static final int CHECK_DISPLAY_ROTATION = 3;
private static final int CLEAR_SCREEN_DELAY = 4;
private static final int UPDATE_RECORD_TIME = 5;
@@ -548,6 +551,7 @@
initializeVideoControl();
mPendingSwitchCameraId = -1;
+ mGpsIndicator = (ImageView) root.findViewById(R.id.onscreen_gps_indicator); // @ +
}
@Override
@@ -3131,5 +3135,32 @@
enableRecordingLocation(false);
}
+ // @ + {
+ @Override
+ public void showGpsOnScreenIndicator(boolean hasSignal) {
+ Log.d(TAG, "showGpsOnScreenIndicator hasSignal = " + hasSignal);
+ if (mGpsIndicator == null) {
+ return;
+ }
+ if (hasSignal) {
+ mGpsIndicator
+ .setImageResource(R.drawable.ic_viewfinder_gps_on);
+ } else {
+ mGpsIndicator
+ .setImageResource(R.drawable.ic_viewfinder_gps_no_signal);
+ }
+ mGpsIndicator.setVisibility(View.VISIBLE);
+ }
+
+ @Override
+ public void hideGpsOnScreenIndicator() {
+ Log.d(TAG, "hideGpsOnScreenIndicator - E");
+ if (mGpsIndicator == null) {
+ return;
+ }
+ mGpsIndicator.setVisibility(View.GONE);
+ }
+ // @ + }
+
}
Index: packages/apps/SnapdragonCamera/src/com/android/camera/LocationManager.java
===================================================================
--- packages/apps/SnapdragonCamera/src/com/android/camera/LocationManager.java (版本 577)
+++ packages/apps/SnapdragonCamera/src/com/android/camera/LocationManager.java (版本 579)
@@ -42,6 +42,10 @@
public interface Listener {
public void onErrorListener(int error);
+ // @ + {
+ public void showGpsOnScreenIndicator(boolean hasSignal);
+ public void hideGpsOnScreenIndicator();
+ // @ + }
}
public LocationManager(Context context, Listener listener) {
@@ -106,6 +110,7 @@
1000,
0F,
mLocationListeners[0]);
+ if (mListener != null) mListener.showGpsOnScreenIndicator(false); // @ +
} catch (SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
if (mListener != null) mListener.onErrorListener(LOC_MNGR_ERR_PERM_DENY);
@@ -129,6 +134,7 @@
}
Log.d(TAG, "stopReceivingLocationUpdates");
}
+ if (mListener != null) mListener.hideGpsOnScreenIndicator(); // @ +
}
private class LocationListener
@@ -149,6 +155,14 @@
// Hack to filter out 0.0,0.0 locations
return;
}
+ // @ + {
+ // If GPS is available before start camera, we won't get status
+ // update so update GPS indicator when we receive data.
+ if (mListener != null && mRecordLocation &&
+ android.location.LocationManager.GPS_PROVIDER.equals(mProvider)) {
+ mListener.showGpsOnScreenIndicator(true);
+ }
+ // @ + }
if (!mValid) {
Log.d(TAG, "Got first location.");
}
@@ -172,6 +186,12 @@
case LocationProvider.OUT_OF_SERVICE:
case LocationProvider.TEMPORARILY_UNAVAILABLE: {
mValid = false;
+ // @ + {
+ if (mListener != null && mRecordLocation &&
+ android.location.LocationManager.GPS_PROVIDER.equals(provider)) {
+ mListener.showGpsOnScreenIndicator(false);
+ }
+ // @ + }
break;
}
}
Index: packages/apps/SnapdragonCamera/src/com/android/camera/CaptureModule.java
===================================================================
--- packages/apps/SnapdragonCamera/src/com/android/camera/CaptureModule.java (版本 577)
+++ packages/apps/SnapdragonCamera/src/com/android/camera/CaptureModule.java (版本 579)
@@ -3687,6 +3687,14 @@
enableRecordingLocation(false);
}
+ // @ + {
+ @Override
+ public void showGpsOnScreenIndicator(boolean hasSignal) {}
+
+ @Override
+ public void hideGpsOnScreenIndicator() {}
+ // @ + }
+
private byte[] getJpegData(Image image) {
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
Index: packages/apps/SnapdragonCamera/res/drawable-mdpi/ic_viewfinder_gps_no_signal.png
===================================================================
无法显示: 文件标记为二进制类型。
svn:mime-type = application/octet-stream
Index: packages/apps/SnapdragonCamera/res/drawable-mdpi/ic_viewfinder_gps_no_signal.png
===================================================================
--- packages/apps/SnapdragonCamera/res/drawable-mdpi/ic_viewfinder_gps_no_signal.png (版本 0)
+++ packages/apps/SnapdragonCamera/res/drawable-mdpi/ic_viewfinder_gps_no_signal.png (版本 579)
属性改变: packages/apps/SnapdragonCamera/res/drawable-mdpi/ic_viewfinder_gps_no_signal.png
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+application/octet-stream
\ No newline at end of property
Index: packages/apps/SnapdragonCamera/res/drawable-mdpi/ic_viewfinder_gps_on.png
===================================================================
无法显示: 文件标记为二进制类型。
svn:mime-type = application/octet-stream
Index: packages/apps/SnapdragonCamera/res/drawable-mdpi/ic_viewfinder_gps_on.png
===================================================================
--- packages/apps/SnapdragonCamera/res/drawable-mdpi/ic_viewfinder_gps_on.png (版本 0)
+++ packages/apps/SnapdragonCamera/res/drawable-mdpi/ic_viewfinder_gps_on.png (版本 579)
属性改变: packages/apps/SnapdragonCamera/res/drawable-mdpi/ic_viewfinder_gps_on.png
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+application/octet-stream
\ No newline at end of property
Index: packages/apps/SnapdragonCamera/res/drawable-mdpi/ic_viewfinder_gps_off.png
===================================================================
无法显示: 文件标记为二进制类型。
svn:mime-type = application/octet-stream
Index: packages/apps/SnapdragonCamera/res/drawable-mdpi/ic_viewfinder_gps_off.png
===================================================================
--- packages/apps/SnapdragonCamera/res/drawable-mdpi/ic_viewfinder_gps_off.png (版本 0)
+++ packages/apps/SnapdragonCamera/res/drawable-mdpi/ic_viewfinder_gps_off.png (版本 579)
属性改变: packages/apps/SnapdragonCamera/res/drawable-mdpi/ic_viewfinder_gps_off.png
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+application/octet-stream
\ No newline at end of property
Index: packages/apps/SnapdragonCamera/res/drawable-hdpi/ic_viewfinder_gps_on.png
===================================================================
无法显示: 文件标记为二进制类型。
svn:mime-type = application/octet-stream
Index: packages/apps/SnapdragonCamera/res/drawable-hdpi/ic_viewfinder_gps_on.png
===================================================================
--- packages/apps/SnapdragonCamera/res/drawable-hdpi/ic_viewfinder_gps_on.png (版本 0)
+++ packages/apps/SnapdragonCamera/res/drawable-hdpi/ic_viewfinder_gps_on.png (版本 579)
属性改变: packages/apps/SnapdragonCamera/res/drawable-hdpi/ic_viewfinder_gps_on.png
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+application/octet-stream
\ No newline at end of property
Index: packages/apps/SnapdragonCamera/res/drawable-hdpi/ic_viewfinder_gps_no_signal.png
===================================================================
无法显示: 文件标记为二进制类型。
svn:mime-type = application/octet-stream
Index: packages/apps/SnapdragonCamera/res/drawable-hdpi/ic_viewfinder_gps_no_signal.png
===================================================================
--- packages/apps/SnapdragonCamera/res/drawable-hdpi/ic_viewfinder_gps_no_signal.png (版本 0)
+++ packages/apps/SnapdragonCamera/res/drawable-hdpi/ic_viewfinder_gps_no_signal.png (版本 579)
属性改变: packages/apps/SnapdragonCamera/res/drawable-hdpi/ic_viewfinder_gps_no_signal.png
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+application/octet-stream
\ No newline at end of property
Index: packages/apps/SnapdragonCamera/res/drawable-hdpi/ic_viewfinder_gps_off.png
===================================================================
无法显示: 文件标记为二进制类型。
svn:mime-type = application/octet-stream
Index: packages/apps/SnapdragonCamera/res/drawable-hdpi/ic_viewfinder_gps_off.png
===================================================================
--- packages/apps/SnapdragonCamera/res/drawable-hdpi/ic_viewfinder_gps_off.png (版本 0)
+++ packages/apps/SnapdragonCamera/res/drawable-hdpi/ic_viewfinder_gps_off.png (版本 579)
属性改变: packages/apps/SnapdragonCamera/res/drawable-hdpi/ic_viewfinder_gps_off.png
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+application/octet-stream
\ No newline at end of property
Index: packages/apps/SnapdragonCamera/res/layout/camera_controls.xml
===================================================================
--- packages/apps/SnapdragonCamera/res/layout/camera_controls.xml (版本 577)
+++ packages/apps/SnapdragonCamera/res/layout/camera_controls.xml (版本 579)
@@ -131,4 +131,22 @@
android:visibility="gone" />
</LinearLayout>
+ <!-- @ + { -->
+ <FrameLayout
+ style="@style/CameraControls"
+ android:layout_gravity="center" >
+
+ <ImageView
+ android:id="@+id/onscreen_gps_indicator"
+ android:visibility="invisible"
+ android:layout_width="@dimen/onscreen_indicators_height"
+ android:layout_height="@dimen/onscreen_indicators_height"
+ android:layout_marginTop="80dip"
+ android:layout_marginRight="20dip"
+ android:scaleType="centerInside"
+ android:layout_gravity="top|right" />
+
+ </FrameLayout>
+ <!-- @ + } -->
+
</com.android.camera.ui.CameraControls>
Index: .
===================================================================
--- . (版本 577)
+++ . (版本 579)
属性改变: .
___________________________________________________________________
Modified: svn:mergeinfo
已经合并 /branch/android_tjh_20200313_c7000_snapcam_gps:r578
即使开启存储位置信息也无法定位成功,因为权限的问题:
ACCESS_FINE_LOCATION包括NETWORK_PROVIDER和GPS_PROVIDER;
ACCESS_COARSE_LOCATION仅包括ETWORK_PROVIDER
Index: packages/apps/SnapdragonCamera/src/com/android/camera/CameraActivity.java
===================================================================
--- packages/apps/SnapdragonCamera/src/com/android/camera/CameraActivity.java (版本 1236)
+++ packages/apps/SnapdragonCamera/src/com/android/camera/CameraActivity.java (工作副本)
@@ -1956,12 +1956,12 @@
}
public void requestLocationPermission() {
- if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
+ if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
Log.v(TAG, "Request Location permission");
mCurrentModule.waitingLocationPermissionResult(true);
requestPermissions(
- new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
+ new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION);
}
}
Index: packages/apps/SnapdragonCamera/src/com/android/camera/PermissionsActivity.java
===================================================================
--- packages/apps/SnapdragonCamera/src/com/android/camera/PermissionsActivity.java (版本 1236)
+++ packages/apps/SnapdragonCamera/src/com/android/camera/PermissionsActivity.java (工作副本)
@@ -84,7 +84,11 @@
mFlagHasStoragePermission = true;
}
- if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
+ /**
+ * ACCESS_FINE_LOCATION include permission for both NETWORK_PROVIDER and GPS_PROVIDER
+ * ACCESS_COARSE_LOCATION include permission only for NETWORK_PROVIDER
+ */
+ if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) // @ modify for BUG #
!= PackageManager.PERMISSION_GRANTED) {
mNumPermissionsToRequest++;
mShouldRequestLocationPermission = true;
@@ -124,7 +128,7 @@
}
if (mShouldRequestLocationPermission) {
permissionsToRequest[permissionsRequestIndex] =
- Manifest.permission.ACCESS_COARSE_LOCATION;
+ Manifest.permission.ACCESS_FINE_LOCATION; // @ modify for BUG #
mIndexPermissionRequestLocation = permissionsRequestIndex;
}
requestPermissions(permissionsToRequest, PERMISSION_REQUEST_CODE);
二、与设置中的定位开关同步
问题:进入相机开启GPS定位,与设置中的GPS开启状态不同步,如设置中的GPS为关闭状态,进入相机开启GPS,设置中的不同步
分析以及解决方案:
以前平台的相机定位功能需要先开启系统的gps开关才能使用,故设计以下解决办法:
当开启存储位置信息后,会弹出提示框,要求用户打开系统gps,点击确定就跳转到设置界面,让用户手动设置,从而实现同步,这样设计的目的是,尽可能降低应用间的耦合性。
如果不点击确定,则无法正常使用相机的存储位置信息功能。
Index: packages/apps/SnapdragonCamera/src/com/android/camera/LocationManager.java
===================================================================
--- packages/apps/SnapdragonCamera/src/com/android/camera/LocationManager.java (版本 468)
+++ packages/apps/SnapdragonCamera/src/com/android/camera/LocationManager.java (版本 471)
@@ -16,12 +16,17 @@
package com.android.camera;
+import android.app.AlertDialog;
import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
import android.location.Location;
import android.location.LocationProvider;
import android.os.Bundle;
import android.util.Log;
+import org.codeaurora.snapcam.R;
+
/**
* A class that handles everything about location.
*/
@@ -64,6 +69,7 @@
if (mRecordLocation != recordLocation) {
mRecordLocation = recordLocation;
if (recordLocation) {
+ showGpsHintDialog(); //@ + for BUG #
startReceivingLocationUpdates();
} else {
stopReceivingLocationUpdates();
@@ -71,6 +77,28 @@
}
}
+ //@ + for BUG # {
+ private void showGpsHintDialog() {
+ if (mLocationManager == null) {
+ mLocationManager = (android.location.LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
+ }
+ boolean isGpsOpened = mLocationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER);
+ if (!isGpsOpened) {
+ AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
+ dialog.setTitle(R.string.gps_title);
+ dialog.setMessage(R.string.gps_message);
+ dialog.setPositiveButton(R.string.dialog_ok, new android.content.DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface arg0, int arg1) {
+ Intent intent = new Intent("android.settings.LOCATION_SOURCE_SETTINGS");
+ mContext.startActivity(intent);
+ }
+ });
+ dialog.show();
+ }
+ }
+ //@ + for BUG # }
+
private void startReceivingLocationUpdates() {
if (mLocationManager == null) {
mLocationManager = (android.location.LocationManager)
Index: packages/apps/SnapdragonCamera/res/values-zh-rCN/strings.xml
===================================================================
--- packages/apps/SnapdragonCamera/res/values-zh-rCN/strings.xml (版本 468)
+++ packages/apps/SnapdragonCamera/res/values-zh-rCN/strings.xml (版本 471)
@@ -540,5 +540,9 @@
<string name="text_tsmakeup_alert_msg">实时美颜需要开启人脸检测功能,是否开启?</string>
<string name="text_tsmakeup_alert_continue">继续</string>
<string name="text_tsmakeup_alert_quit">退出</string>
+
+ <!-- @ + for BUG # -->
+ <string name="gps_title">GPS提示</string>
+ <string name="gps_message">请打开gps开关,以便您能正常使用存储位置信息功能</string>
</resources>
Index: packages/apps/SnapdragonCamera/res/values-zh-rTW/strings.xml
===================================================================
--- packages/apps/SnapdragonCamera/res/values-zh-rTW/strings.xml (版本 468)
+++ packages/apps/SnapdragonCamera/res/values-zh-rTW/strings.xml (版本 471)
@@ -293,4 +293,8 @@
<string name="switch_photo_filmstrip" msgid="1448511001008888767">"幻燈片檢視"</string>
<string name="setting_wallpaper" msgid="2397759659347872725">"正在設定桌布"</string>
<string name="pref_camera_longshot_title">連拍</string>
+
+ <!-- @ + for BUG # -->
+ <string name="gps_title">GPS提示</string>
+ <string name="gps_message">請打開gps開關,以便您能正常使用存儲位置信息功能</string>
</resources>
Index: packages/apps/SnapdragonCamera/res/values/strings.xml
===================================================================
--- packages/apps/SnapdragonCamera/res/values/strings.xml (版本 468)
+++ packages/apps/SnapdragonCamera/res/values/strings.xml (版本 471)
@@ -718,4 +718,8 @@
<string name="remaining_photos_format">%d left</string>
<string name="refocus_toast">To re-focus last photo, tap here</string>
+
+ <!-- @ + for BUG # -->
+ <string name="gps_title">GPS notification</string>
+ <string name="gps_message">Please enable GPS to store location</string>
</resources>
Index: packages/apps/SnapdragonCamera/res/values-zh-rHK/strings.xml
===================================================================
--- packages/apps/SnapdragonCamera/res/values-zh-rHK/strings.xml (版本 468)
+++ packages/apps/SnapdragonCamera/res/values-zh-rHK/strings.xml (版本 471)
@@ -292,4 +292,8 @@
<string name="cannot_load_image" msgid="4100136187076585580">"無法載入相片!"</string>
<string name="switch_photo_filmstrip" msgid="1448511001008888767">"幻燈片檢視"</string>
<string name="setting_wallpaper" msgid="2397759659347872725">"正在設定桌布"</string>
+
+ <!-- @ + for BUG # -->
+ <string name="gps_title">GPS提示</string>
+ <string name="gps_message">請打開gps開關,以便您能正常使用存儲位置信息功能</string>
</resources>
上一篇: Java JVM 内存解析