This class provides a user profile.
It handles the user's following information:
- Date of birth
- Gender
- Height
- Weight
- Nickname
Getting User Profile
Samsung Health's user profile is read-only and you can get it after acquiring the user's permission.
Set the user profile data type's permission info in the manifest.
<manifest . . . >
<application . . . >
<meta-data android:name="com.samsung.android.health.permission.read"
android:value="com.samsung.health.user_profile"/>
</application . . . >
</manifest . . . >
Getting the user profile is same with other data type access.
Returning an empty string, 0.0, or unknown value can be retrieved for each property
means that the user doesn't fill out Samsung Health's profile.
public class MainActivity extends Activity {
// The state of connection
private HealthDataStore mStore;
// Permission key set
Set<HealthPermissionManager.PermissionKey> mKeySet = new HashSet<HealthPermissionManager.PermissionKey>();
public static final String APP_TAG = "MyApp";
private void getUserProfile() {
// Adds a permission to read a user profile to the permission key set
mKeySet.add(new HealthPermissionManager.PermissionKey(HealthConstants.USER_PROFILE_DATA_TYPE,
HealthPermissionManager.PermissionType.READ));
HealthPermissionManager pmsManager = new HealthPermissionManager(mStore);
try {
// Checks if a user profile permission is acquired
Map<HealthPermissionManager.PermissionKey, Boolean> keyMap = pmsManager.isPermissionAcquired(mKeySet);
// Request permission if the user profile permission is not acquired
if(keyMap.containsValue(Boolean.FALSE)) {
pmsManager.requestPermissions(mKeySet, MainActivity.this).setResultListener(mPermissionListener);
}
} catch(Exception e) {
Log.d(APP_TAG, "requestPermissions() fails.");
}
}
private final HealthResultHolder.ResultListener<HealthPermissionManager.PermissionResult> mPermissionListener
= new HealthResultHolder.ResultListener<HealthPermissionManager.PermissionResult>() {
@Override
public void onResult(HealthPermissionManager.PermissionResult permissionResult) {
Map<HealthPermissionManager.PermissionKey, Boolean> resultMap = permissionResult.getResultMap();
if (resultMap.containsValue(Boolean.TRUE)) {
// Gets the user profile if permission is acquired
try {
HealthUserProfile usrProfile = HealthUserProfile.getProfile(mStore);
// Date of birth - yyyymmdd
String birthDate = usrProfile.getBirthDate();
if (birthDate.isEmpty()) {
Log.d(APP_TAG, "Date of birth is not set by the user yet.");
}
// Gender
int gender = usrProfile.getGender();
if (gender == HealthUserProfile.GENDER_UNKNOWN) {
Log.d(APP_TAG, "Gender is not set by the user yet.");
}
// Height
float height = usrProfile.getHeight();
if (height == 0.0f) {
Log.d(APP_TAG, "Height is not set by the user yet.");
}
// Weight
float weight = usrProfile.getWeight();
if (weight == 0.0f) {
Log.d(APP_TAG, "Weight is not set by the user yet.");
}
} else {
Log.d(TAG, "Fail to get permission.");
}
}
};
}