Home | History | Annotate | Download | only in support
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      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 
     17 package com.android.settings.support;
     18 
     19 import android.content.Intent;
     20 import android.net.Uri;
     21 import android.os.Parcel;
     22 import android.os.Parcelable;
     23 import android.text.TextUtils;
     24 
     25 import java.text.ParseException;
     26 
     27 /**
     28  * Data model for a support phone number.
     29  */
     30 public final class SupportPhone implements Parcelable {
     31 
     32     public final String language;
     33     public final String number;
     34     public final boolean isTollFree;
     35 
     36     public SupportPhone(String config) throws ParseException {
     37         // Config follows this format: language:[tollfree|tolled]:number
     38         final String[] tokens = config.split(":");
     39         if (tokens.length != 3) {
     40             throw new ParseException("Phone config is invalid " + config, 0);
     41         }
     42         language = tokens[0];
     43         isTollFree = TextUtils.equals(tokens[1], "tollfree");
     44         number = tokens[2];
     45     }
     46 
     47     protected SupportPhone(Parcel in) {
     48         language = in.readString();
     49         number = in.readString();
     50         isTollFree = in.readInt() != 0;
     51     }
     52 
     53     public Intent getDialIntent() {
     54         return new Intent(Intent.ACTION_DIAL)
     55                 .setData(new Uri.Builder()
     56                         .scheme("tel")
     57                         .appendPath(number)
     58                         .build());
     59     }
     60 
     61     @Override
     62     public int describeContents() {
     63         return 0;
     64     }
     65 
     66     @Override
     67     public void writeToParcel(Parcel dest, int flags) {
     68         dest.writeString(language);
     69         dest.writeString(number);
     70         dest.writeInt(isTollFree ? 1 : 0);
     71     }
     72 
     73     public static final Creator<SupportPhone> CREATOR = new Creator<SupportPhone>() {
     74         @Override
     75         public SupportPhone createFromParcel(Parcel in) {
     76             return new SupportPhone(in);
     77         }
     78 
     79         @Override
     80         public SupportPhone[] newArray(int size) {
     81             return new SupportPhone[size];
     82         }
     83     };
     84 }
     85