Home | History | Annotate | Download | only in dictionarypack
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * 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, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * the License.
     15  */
     16 
     17 package com.android.inputmethod.dictionarypack;
     18 
     19 import android.app.Activity;
     20 import android.content.Intent;
     21 import android.os.Bundle;
     22 import android.text.Html;
     23 import android.view.View;
     24 import android.widget.Button;
     25 import android.widget.TextView;
     26 
     27 import com.android.inputmethod.latin.R;
     28 
     29 import java.util.Locale;
     30 
     31 /**
     32  * This implements the dialog for asking the user whether it's okay to download dictionaries over
     33  * a metered connection or not (e.g. their mobile data plan).
     34  */
     35 public final class DownloadOverMeteredDialog extends Activity {
     36     final public static String CLIENT_ID_KEY = "client_id";
     37     final public static String WORDLIST_TO_DOWNLOAD_KEY = "wordlist_to_download";
     38     final public static String SIZE_KEY = "size";
     39     final public static String LOCALE_KEY = "locale";
     40     private String mClientId;
     41     private String mWordListToDownload;
     42 
     43     @Override
     44     protected void onCreate(final Bundle savedInstanceState) {
     45         super.onCreate(savedInstanceState);
     46         final Intent intent = getIntent();
     47         mClientId = intent.getStringExtra(CLIENT_ID_KEY);
     48         mWordListToDownload = intent.getStringExtra(WORDLIST_TO_DOWNLOAD_KEY);
     49         final String localeString = intent.getStringExtra(LOCALE_KEY);
     50         final long size = intent.getIntExtra(SIZE_KEY, 0);
     51         setContentView(R.layout.download_over_metered);
     52         setTexts(localeString, size);
     53     }
     54 
     55     private void setTexts(final String localeString, final long size) {
     56         final String promptFormat = getString(R.string.should_download_over_metered_prompt);
     57         final String allowButtonFormat = getString(R.string.download_over_metered);
     58         final Locale locale = LocaleUtils.constructLocaleFromString(localeString);
     59         final String language = (null == locale ? "" : locale.getDisplayLanguage());
     60         final TextView prompt = (TextView)findViewById(R.id.download_over_metered_prompt);
     61         prompt.setText(Html.fromHtml(String.format(promptFormat, language)));
     62         final Button allowButton = (Button)findViewById(R.id.allow_button);
     63         allowButton.setText(String.format(allowButtonFormat, ((float)size)/(1024*1024)));
     64     }
     65 
     66     public void onClickDeny(final View v) {
     67         UpdateHandler.setDownloadOverMeteredSetting(this, false);
     68         finish();
     69     }
     70 
     71     public void onClickAllow(final View v) {
     72         UpdateHandler.setDownloadOverMeteredSetting(this, true);
     73         UpdateHandler.installIfNeverRequested(this, mClientId, mWordListToDownload,
     74                 false /* mayPrompt */);
     75         finish();
     76     }
     77 }
     78