1 /* 2 * Copyright (C) 2017 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 package android.fragment.cts; 17 18 import android.app.LoaderManager; 19 import android.content.AsyncTaskLoader; 20 import android.content.Context; 21 import android.content.Loader; 22 import android.os.Bundle; 23 import android.view.ViewGroup; 24 import android.widget.TextView; 25 26 import java.util.concurrent.CountDownLatch; 27 28 /** 29 * This Activity sets the text when loading completes. It also tracks the Activity in 30 * a static variable, so it must be cleared in test tear down. 31 */ 32 public class LoaderActivity extends RecreatedActivity { 33 public TextView textView; 34 public TextView textViewB; 35 public CountDownLatch loadFinished = new CountDownLatch(1); 36 37 @Override 38 protected void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 41 setContentView(R.layout.text_a); 42 textView = (TextView) findViewById(R.id.textA); 43 44 ViewGroup container = (ViewGroup) textView.getParent(); 45 textViewB = new TextView(this); 46 textViewB.setId(R.id.textB); 47 container.addView(textViewB); 48 } 49 50 @Override 51 protected void onResume() { 52 super.onResume(); 53 getLoaderManager().initLoader(0, null, new TextLoaderCallback()); 54 } 55 56 class TextLoaderCallback implements LoaderManager.LoaderCallbacks<String> { 57 @Override 58 public Loader<String> onCreateLoader(int id, Bundle args) { 59 return new TextLoader(LoaderActivity.this); 60 } 61 62 @Override 63 public void onLoadFinished(Loader<String> loader, String data) { 64 textView.setText(data); 65 loadFinished.countDown(); 66 } 67 68 @Override 69 public void onLoaderReset(Loader<String> loader) { 70 } 71 } 72 73 static class TextLoader extends AsyncTaskLoader<String> { 74 TextLoader(Context context) { 75 super(context); 76 } 77 78 @Override 79 protected void onStartLoading() { 80 forceLoad(); 81 } 82 83 @Override 84 public String loadInBackground() { 85 return "Loaded!"; 86 } 87 } 88 } 89 90