Home | History | Annotate | Download | only in model
      1 /*
      2  * Copyright 2017 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.example.android.wearable.wear.messaging.model;
     17 
     18 import android.os.Parcel;
     19 import android.os.Parcelable;
     20 import android.support.annotation.NonNull;
     21 import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
     22 
     23 /** Represents a user profile. Parcelable to pass between activities. */
     24 public class Profile implements Parcelable {
     25 
     26     private String id;
     27     private String email;
     28     private String name;
     29     private String profileImageUri;
     30     private int profileImageResource;
     31     private Long lastUpdatedTime;
     32 
     33     public Profile() {}
     34 
     35     public Profile(@NonNull GoogleSignInAccount account) {
     36         this.id = account.getId();
     37         this.email = account.getEmail();
     38         this.name = account.getDisplayName();
     39         this.profileImageUri =
     40                 (account.getPhotoUrl() != null) ? account.getPhotoUrl().toString() : null;
     41         this.lastUpdatedTime = System.currentTimeMillis();
     42     }
     43 
     44     public Profile(String id, String name, String imageUri) {
     45         this.id = id;
     46         this.name = name;
     47         this.profileImageUri = imageUri;
     48         this.lastUpdatedTime = System.currentTimeMillis();
     49     }
     50 
     51     private Profile(Builder builder) {
     52         setId(builder.id);
     53         setEmail(builder.email);
     54         setName(builder.name);
     55         setProfileImageResource(builder.profileImageResource);
     56         setLastUpdatedTime(builder.lastUpdatedTime);
     57     }
     58 
     59     public String getId() {
     60         return id;
     61     }
     62 
     63     public void setId(String id) {
     64         this.id = id;
     65     }
     66 
     67     public void setName(String name) {
     68         this.name = name;
     69     }
     70 
     71     public String getName() {
     72         return name;
     73     }
     74 
     75     public int getProfileImageResource() {
     76         return profileImageResource;
     77     }
     78 
     79     public void setProfileImageResource(int profileImageResource) {
     80         this.profileImageResource = profileImageResource;
     81     }
     82 
     83     public void setProfileImageUri(String profileImageUri) {
     84         this.profileImageUri = profileImageUri;
     85     }
     86 
     87     public String getProfileImageUri() {
     88         return profileImageUri;
     89     }
     90 
     91     public Object getProfileImageSource() {
     92         if (profileImageUri != null) {
     93             return profileImageUri;
     94         }
     95         if (profileImageResource > 0) {
     96             return profileImageResource;
     97         }
     98         return null;
     99     }
    100 
    101     public String getEmail() {
    102         return email;
    103     }
    104 
    105     public void setEmail(String email) {
    106         this.email = email;
    107     }
    108 
    109     public Long getLastUpdatedTime() {
    110         return lastUpdatedTime;
    111     }
    112 
    113     public void setLastUpdatedTime(Long lastUpdatedTime) {
    114         this.lastUpdatedTime = lastUpdatedTime;
    115     }
    116 
    117     /**
    118      * Indicates whether some other object is "equal to" this one.
    119      *
    120      * @param other - the reference object with which to compare.
    121      * @return true/false based on all fields being equal or equally null
    122      */
    123     @Override
    124     public boolean equals(Object other) {
    125         if (this == other) {
    126             return true;
    127         }
    128         if (other == null || getClass() != other.getClass()) {
    129             return false;
    130         }
    131 
    132         Profile profile = (Profile) other;
    133 
    134         if (!id.equals(profile.id)) {
    135             return false;
    136         }
    137         if (email != null ? !email.equals(profile.email) : profile.email != null) {
    138             return false;
    139         }
    140         if (!name.equals(profile.name)) {
    141             return false;
    142         }
    143         if (profileImageUri != null && !profileImageUri.equals(profile.profileImageUri)) {
    144             return false;
    145         }
    146         return lastUpdatedTime != null
    147                 ? lastUpdatedTime.equals(profile.lastUpdatedTime)
    148                 : profile.lastUpdatedTime == null;
    149     }
    150 
    151     /**
    152      * Returns a hash code value for the object.
    153      *
    154      * @return a hash code value for this object.
    155      */
    156     @Override
    157     public int hashCode() {
    158         int result = id.hashCode();
    159         result = 31 * result + (email != null ? email.hashCode() : 0);
    160         result = 31 * result + name.hashCode();
    161         result = 31 * result + profileImageUri.hashCode();
    162         result = 31 * result + (lastUpdatedTime != null ? lastUpdatedTime.hashCode() : 0);
    163         return result;
    164     }
    165 
    166     /** Builder crates profiles. */
    167     public static final class Builder {
    168         private String id;
    169         private String email;
    170         private String name;
    171         private int profileImageResource;
    172         private Long lastUpdatedTime;
    173 
    174         public Builder() {}
    175 
    176         public Builder id(String id) {
    177             this.id = id;
    178             return this;
    179         }
    180 
    181         public Builder email(String email) {
    182             this.email = email;
    183             return this;
    184         }
    185 
    186         public Builder name(String name) {
    187             this.name = name;
    188             return this;
    189         }
    190 
    191         public Builder profileImageResource(int profileImageResource) {
    192             this.profileImageResource = profileImageResource;
    193             return this;
    194         }
    195 
    196         public Builder lastUpdatedTime(Long lastUpdatedTime) {
    197             this.lastUpdatedTime = lastUpdatedTime;
    198             return this;
    199         }
    200 
    201         public Profile build() {
    202             return new Profile(this);
    203         }
    204     }
    205 
    206     @Override
    207     public int describeContents() {
    208         return 0;
    209     }
    210 
    211     @Override
    212     public void writeToParcel(Parcel dest, int flags) {
    213         dest.writeString(this.id);
    214         dest.writeString(this.email);
    215         dest.writeString(this.name);
    216         dest.writeString(this.profileImageUri);
    217         dest.writeInt(this.profileImageResource);
    218         dest.writeValue(this.lastUpdatedTime);
    219     }
    220 
    221     protected Profile(Parcel in) {
    222         this.id = in.readString();
    223         this.email = in.readString();
    224         this.name = in.readString();
    225         this.profileImageUri = in.readString();
    226         this.profileImageResource = in.readInt();
    227         this.lastUpdatedTime = (Long) in.readValue(Long.class.getClassLoader());
    228     }
    229 
    230     public static final Creator<Profile> CREATOR =
    231             new Creator<Profile>() {
    232                 @Override
    233                 public Profile createFromParcel(Parcel source) {
    234                     return new Profile(source);
    235                 }
    236 
    237                 @Override
    238                 public Profile[] newArray(int size) {
    239                     return new Profile[size];
    240                 }
    241             };
    242 }
    243