Add prebuilt browseable samples as static files.

Change-Id: Ifb5382223343400882834d2dd9c182c3df602e34
This commit is contained in:
Dirk Dougherty
2013-10-29 20:56:17 -07:00
parent 7165109e6d
commit 4b737b695e
840 changed files with 35304 additions and 0 deletions

View File

@@ -0,0 +1,131 @@
/*
* Copyright 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.basicnetworking;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import com.example.android.common.logger.Log;
import com.example.android.common.logger.LogFragment;
import com.example.android.common.logger.LogWrapper;
import com.example.android.common.logger.MessageOnlyLogFilter;
/**
* Sample application demonstrating how to test whether a device is connected,
* and if so, whether the connection happens to be wifi or mobile (it could be
* something else).
*
* This sample uses the logging framework to display log output in the log
* fragment (LogFragment).
*/
public class MainActivity extends FragmentActivity {
public static final String TAG = "Basic Network Demo";
// Whether there is a Wi-Fi connection.
private static boolean wifiConnected = false;
// Whether there is a mobile connection.
private static boolean mobileConnected = false;
// Reference to the fragment showing events, so we can clear it with a button
// as necessary.
private LogFragment mLogFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_main);
// Initialize text fragment that displays intro text.
SimpleTextFragment introFragment = (SimpleTextFragment)
getSupportFragmentManager().findFragmentById(R.id.intro_fragment);
introFragment.setText(R.string.intro_message);
introFragment.getTextView().setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16.0f);
// Initialize the logging framework.
initializeLogging();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// When the user clicks TEST, display the connection status.
case R.id.test_action:
checkNetworkConnection();
return true;
// Clear the log view fragment.
case R.id.clear_action:
mLogFragment.getLogView().setText("");
return true;
}
return false;
}
/**
* Check whether the device is connected, and if so, whether the connection
* is wifi or mobile (it could be something else).
*/
private void checkNetworkConnection() {
// BEGIN_INCLUDE(connect)
ConnectivityManager connMgr =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
if (activeInfo != null && activeInfo.isConnected()) {
wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
if(wifiConnected) {
Log.i(TAG, getString(R.string.wifi_connection));
} else if (mobileConnected){
Log.i(TAG, getString(R.string.mobile_connection));
}
} else {
Log.i(TAG, getString(R.string.no_wifi_or_mobile));
}
// END_INCLUDE(connect)
}
/** Create a chain of targets that will receive log data */
public void initializeLogging() {
// Using Log, front-end to the logging chain, emulates
// android.util.log method signatures.
// Wraps Android's native log framework
LogWrapper logWrapper = new LogWrapper();
Log.setLogNode(logWrapper);
// A filter that strips out everything except the message text.
MessageOnlyLogFilter msgFilter = new MessageOnlyLogFilter();
logWrapper.setNext(msgFilter);
// On screen logging via a fragment with a TextView.
mLogFragment =
(LogFragment) getSupportFragmentManager().findFragmentById(R.id.log_fragment);
msgFilter.setNext(mLogFragment.getLogView());
}
}

View File

@@ -0,0 +1,99 @@
/*
* Copyright 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.basicnetworking;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Simple fragment containing only a TextView. Used by TextPagerAdapter to create
* tutorial-style pages for apps.
*/
public class SimpleTextFragment extends Fragment {
// Contains the text that will be displayed by this Fragment
String mText;
// Contains a resource ID for the text that will be displayed by this fragment.
int mTextId = -1;
// Keys which will be used to store/retrieve text passed in via setArguments.
public static final String TEXT_KEY = "text";
public static final String TEXT_ID_KEY = "text_id";
// For situations where the app wants to modify text at Runtime, exposing the TextView.
private TextView mTextView;
public SimpleTextFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Before initializing the textView, check if any arguments were provided via setArguments.
processArguments();
// Create a new TextView and set its text to whatever was provided.
mTextView = new TextView(getActivity());
mTextView.setGravity(Gravity.CENTER);
if (mText != null) {
mTextView.setText(mText);
Log.i("SimpleTextFragment", mText);
}
return mTextView;
}
public TextView getTextView() {
return mTextView;
}
/**
* Changes the text for this TextView, according to the resource ID provided.
* @param stringId A resource ID representing the text content for this Fragment's TextView.
*/
public void setText(int stringId) {
getTextView().setText(getActivity().getString(stringId));
}
/**
* Processes the arguments passed into this Fragment via setArguments method.
* Currently the method only looks for text or a textID, nothing else.
*/
public void processArguments() {
// For most objects we'd handle the multiple possibilities for initialization variables
// as multiple constructors. For Fragments, however, it's customary to use
// setArguments / getArguments.
if (getArguments() != null) {
Bundle args = getArguments();
if (args.containsKey(TEXT_KEY)) {
mText = args.getString(TEXT_KEY);
Log.d("Constructor", "Added Text.");
} else if (args.containsKey(TEXT_ID_KEY)) {
mTextId = args.getInt(TEXT_ID_KEY);
mText = getString(mTextId);
}
}
}
}