Home | History | Annotate | Download | only in setup
      1 /*
      2  * Copyright (C) 2009 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.email.activity.setup;
     18 
     19 import com.android.email.AccountBackupRestore;
     20 import com.android.email.Email;
     21 import com.android.email.R;
     22 import com.android.email.VendorPolicyLoader;
     23 import com.android.email.provider.EmailContent;
     24 import com.android.email.provider.EmailContent.AccountColumns;
     25 
     26 import android.content.ContentValues;
     27 import android.content.Context;
     28 import android.content.res.XmlResourceParser;
     29 import android.util.Log;
     30 
     31 import java.io.Serializable;
     32 import java.net.URI;
     33 
     34 public class AccountSettingsUtils {
     35 
     36     /**
     37      * Commits the UI-related settings of an account to the provider.  This is static so that it
     38      * can be used by the various account activities.  If the account has never been saved, this
     39      * method saves it; otherwise, it just saves the settings.
     40      * @param context the context of the caller
     41      * @param account the account whose settings will be committed
     42      */
     43     public static void commitSettings(Context context, EmailContent.Account account) {
     44         if (!account.isSaved()) {
     45             account.save(context);
     46         } else {
     47             ContentValues cv = new ContentValues();
     48             cv.put(AccountColumns.IS_DEFAULT, account.mIsDefault);
     49             cv.put(AccountColumns.DISPLAY_NAME, account.getDisplayName());
     50             cv.put(AccountColumns.SENDER_NAME, account.getSenderName());
     51             cv.put(AccountColumns.SIGNATURE, account.getSignature());
     52             cv.put(AccountColumns.SYNC_INTERVAL, account.mSyncInterval);
     53             cv.put(AccountColumns.RINGTONE_URI, account.mRingtoneUri);
     54             cv.put(AccountColumns.FLAGS, account.mFlags);
     55             cv.put(AccountColumns.SYNC_LOOKBACK, account.mSyncLookback);
     56             account.update(context, cv);
     57         }
     58         // Update the backup (side copy) of the accounts
     59         AccountBackupRestore.backupAccounts(context);
     60     }
     61 
     62     /**
     63      * Search the list of known Email providers looking for one that matches the user's email
     64      * domain.  We check for vendor supplied values first, then we look in providers_product.xml,
     65      * and finally by the entries in platform providers.xml.  This provides a nominal override
     66      * capability.
     67      *
     68      * A match is defined as any provider entry for which the "domain" attribute matches.
     69      *
     70      * @param domain The domain portion of the user's email address
     71      * @return suitable Provider definition, or null if no match found
     72      */
     73     public static Provider findProviderForDomain(Context context, String domain) {
     74         Provider p = VendorPolicyLoader.getInstance(context).findProviderForDomain(domain);
     75         if (p == null) {
     76             p = findProviderForDomain(context, domain, R.xml.providers_product);
     77         }
     78         if (p == null) {
     79             p = findProviderForDomain(context, domain, R.xml.providers);
     80         }
     81         return p;
     82     }
     83 
     84     /**
     85      * Search a single resource containing known Email provider definitions.
     86      *
     87      * @param domain The domain portion of the user's email address
     88      * @param resourceId Id of the provider resource to scan
     89      * @return suitable Provider definition, or null if no match found
     90      */
     91     private static Provider findProviderForDomain(Context context, String domain, int resourceId) {
     92         try {
     93             XmlResourceParser xml = context.getResources().getXml(resourceId);
     94             int xmlEventType;
     95             Provider provider = null;
     96             while ((xmlEventType = xml.next()) != XmlResourceParser.END_DOCUMENT) {
     97                 if (xmlEventType == XmlResourceParser.START_TAG
     98                         && "provider".equals(xml.getName())
     99                         && domain.equalsIgnoreCase(getXmlAttribute(context, xml, "domain"))) {
    100                     provider = new Provider();
    101                     provider.id = getXmlAttribute(context, xml, "id");
    102                     provider.label = getXmlAttribute(context, xml, "label");
    103                     provider.domain = getXmlAttribute(context, xml, "domain");
    104                     provider.note = getXmlAttribute(context, xml, "note");
    105                 }
    106                 else if (xmlEventType == XmlResourceParser.START_TAG
    107                         && "incoming".equals(xml.getName())
    108                         && provider != null) {
    109                     provider.incomingUriTemplate = new URI(getXmlAttribute(context, xml, "uri"));
    110                     provider.incomingUsernameTemplate = getXmlAttribute(context, xml, "username");
    111                 }
    112                 else if (xmlEventType == XmlResourceParser.START_TAG
    113                         && "outgoing".equals(xml.getName())
    114                         && provider != null) {
    115                     provider.outgoingUriTemplate = new URI(getXmlAttribute(context, xml, "uri"));
    116                     provider.outgoingUsernameTemplate = getXmlAttribute(context, xml, "username");
    117                 }
    118                 else if (xmlEventType == XmlResourceParser.END_TAG
    119                         && "provider".equals(xml.getName())
    120                         && provider != null) {
    121                     return provider;
    122                 }
    123             }
    124         }
    125         catch (Exception e) {
    126             Log.e(Email.LOG_TAG, "Error while trying to load provider settings.", e);
    127         }
    128         return null;
    129     }
    130 
    131     /**
    132      * Attempts to get the given attribute as a String resource first, and if it fails
    133      * returns the attribute as a simple String value.
    134      * @param xml
    135      * @param name
    136      * @return the requested resource
    137      */
    138     private static String getXmlAttribute(Context context, XmlResourceParser xml, String name) {
    139         int resId = xml.getAttributeResourceValue(null, name, 0);
    140         if (resId == 0) {
    141             return xml.getAttributeValue(null, name);
    142         }
    143         else {
    144             return context.getString(resId);
    145         }
    146     }
    147 
    148     public static class Provider implements Serializable {
    149         private static final long serialVersionUID = 8511656164616538989L;
    150 
    151         public String id;
    152         public String label;
    153         public String domain;
    154         public URI incomingUriTemplate;
    155         public String incomingUsernameTemplate;
    156         public URI outgoingUriTemplate;
    157         public String outgoingUsernameTemplate;
    158         public String note;
    159     }
    160 }
    161