Home | History | Annotate | Download | only in cts
      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 android.webkit.cts;
     18 
     19 import android.app.Activity;
     20 import android.os.Bundle;
     21 import android.view.ViewGroup;
     22 import android.view.ViewParent;
     23 import android.webkit.CookieSyncManager;
     24 import android.webkit.WebView;
     25 
     26 import com.android.compatibility.common.util.NullWebViewUtils;
     27 
     28 public class CookieSyncManagerCtsActivity extends Activity {
     29     private WebView mWebView;
     30 
     31     @Override
     32     public void onCreate(Bundle savedInstanceState) {
     33         super.onCreate(savedInstanceState);
     34 
     35         try {
     36             CookieSyncManager.createInstance(this);
     37 
     38             mWebView = new WebView(this);
     39             setContentView(mWebView);
     40         } catch (Exception e) {
     41             NullWebViewUtils.determineIfWebViewAvailable(this, e);
     42         }
     43     }
     44 
     45     @Override
     46     protected void onResume() {
     47         super.onResume();
     48         try {
     49             CookieSyncManager.getInstance().startSync();
     50         } catch (Exception e) {
     51             // May throw on a device with no webview, OK to ignore at this point.
     52         }
     53     }
     54 
     55     @Override
     56     public void onDestroy() {
     57         if (mWebView != null) {
     58             ViewParent parent =  mWebView.getParent();
     59             if (parent instanceof ViewGroup) {
     60                 ((ViewGroup) parent).removeView(mWebView);
     61             }
     62             mWebView.destroy();
     63         }
     64         super.onDestroy();
     65     }
     66 
     67     @Override
     68     protected void onStop() {
     69         super.onStop();
     70         try {
     71             CookieSyncManager.getInstance().stopSync();
     72         } catch (Exception e) {
     73             // May throw on a device with no webview, OK to ignore at this point.
     74         }
     75     }
     76 
     77     public WebView getWebView(){
     78         return mWebView;
     79     }
     80 }
     81