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.annotations.ExternallyReferenced;
     28 import com.android.inputmethod.latin.R;
     29 import com.android.inputmethod.latin.common.LocaleUtils;
     30 
     31 import javax.annotation.Nullable;
     32 
     33 /**
     34  * This implements the dialog for asking the user whether it's okay to download dictionaries over
     35  * a metered connection or not (e.g. their mobile data plan).
     36  */
     37 public final class DownloadOverMeteredDialog extends Activity {
     38     final public static String CLIENT_ID_KEY = "client_id";
     39     final public static String WORDLIST_TO_DOWNLOAD_KEY = "wordlist_to_download";
     40     final public static String SIZE_KEY = "size";
     41     final public static String LOCALE_KEY = "locale";
     42     private String mClientId;
     43     private String mWordListToDownload;
     44 
     45     @Override
     46     protected void onCreate(final Bundle savedInstanceState) {
     47         super.onCreate(savedInstanceState);
     48         final Intent intent = getIntent();
     49         mClientId = intent.getStringExtra(CLIENT_ID_KEY);
     50         mWordListToDownload = intent.getStringExtra(WORDLIST_TO_DOWNLOAD_KEY);
     51         final String localeString = intent.getStringExtra(LOCALE_KEY);
     52         final long size = intent.getIntExtra(SIZE_KEY, 0);
     53         setContentView(R.layout.download_over_metered);
     54         setTexts(localeString, size);
     55     }
     56 
     57     private void setTexts(@Nullable final String localeString, final long size) {
     58         final String promptFormat = getString(R.string.should_download_over_metered_prompt);
     59         final String allowButtonFormat = getString(R.string.download_over_metered);
     60         final String language = (null == localeString) ? ""
     61                 : LocaleUtils.constructLocaleFromString(localeString).getDisplayLanguage();
     62         final TextView prompt = (TextView)findViewById(R.id.download_over_metered_prompt);
     63         prompt.setText(Html.fromHtml(String.format(promptFormat, language)));
     64         final Button allowButton = (Button)findViewById(R.id.allow_button);
     65         allowButton.setText(String.format(allowButtonFormat, ((float)size)/(1024*1024)));
     66     }
     67 
     68     // This method is externally referenced from layout/download_over_metered.xml using onClick
     69     // attribute of Button.
     70     @ExternallyReferenced
     71     @SuppressWarnings("unused")
     72     public void onClickDeny(final View v) {
     73         UpdateHandler.setDownloadOverMeteredSetting(this, false);
     74         finish();
     75     }
     76 
     77     // This method is externally referenced from layout/download_over_metered.xml using onClick
     78     // attribute of Button.
     79     @ExternallyReferenced
     80     @SuppressWarnings("unused")
     81     public void onClickAllow(final View v) {
     82         UpdateHandler.setDownloadOverMeteredSetting(this, true);
     83         UpdateHandler.installIfNeverRequested(this, mClientId, mWordListToDownload);
     84         finish();
     85     }
     86 }
     87