MasterChooser.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.android;
00018 
00019 import com.google.common.base.Preconditions;
00020 
00021 import android.app.Activity;
00022 import android.content.Intent;
00023 import android.content.SharedPreferences;
00024 import android.content.pm.PackageManager;
00025 import android.content.pm.ResolveInfo;
00026 import android.net.Uri;
00027 import android.os.Bundle;
00028 import android.view.View;
00029 import android.widget.Button;
00030 import android.widget.CheckBox;
00031 import android.widget.EditText;
00032 import android.widget.Toast;
00033 import org.ros.android.android_gingerbread_mr1.R;
00034 import org.ros.node.NodeConfiguration;
00035 
00036 import java.net.URI;
00037 import java.net.URISyntaxException;
00038 import java.util.List;
00039 
00051 public class MasterChooser extends Activity {
00052 
00057   private static final String PREFS_KEY_NAME = "URI_KEY";
00058 
00062   private static final String BAR_CODE_SCANNER_PACKAGE_NAME =
00063       "com.google.zxing.client.android.SCAN";
00064 
00065   private String masterUri;
00066   private EditText uriText;
00067 
00068   @Override
00069   protected void onCreate(Bundle savedInstanceState) {
00070     super.onCreate(savedInstanceState);
00071     setContentView(R.layout.master_chooser);
00072     uriText = (EditText) findViewById(R.id.master_chooser_uri);
00073     // Get the URI from preferences and display it. Since only primitive types
00074     // can be saved in preferences the URI is stored as a string.
00075     masterUri =
00076         getPreferences(MODE_PRIVATE).getString(PREFS_KEY_NAME,
00077             NodeConfiguration.DEFAULT_MASTER_URI.toString());
00078     uriText.setText(masterUri);
00079   }
00080 
00081   @Override
00082   public void onActivityResult(int requestCode, int resultCode, Intent intent) {
00083     // If the Barcode Scanner returned a string then display that string.
00084     if (requestCode == 0) {
00085       if (resultCode == RESULT_OK) {
00086         String scanResultFormat = intent.getStringExtra("SCAN_RESULT_FORMAT");
00087         Preconditions.checkState(scanResultFormat.equals("TEXT_TYPE") || scanResultFormat.equals("QR_CODE"));
00088         String contents = intent.getStringExtra("SCAN_RESULT");
00089         uriText.setText(contents);
00090       }
00091     }
00092   }
00093 
00094   public void okButtonClicked(View unused) {
00095     // Get the current text entered for URI.
00096     String userUri = uriText.getText().toString();
00097 
00098     if (userUri.length() == 0) {
00099       // If there is no text input then set it to the default URI.
00100       userUri = NodeConfiguration.DEFAULT_MASTER_URI.toString();
00101       uriText.setText(userUri);
00102       Toast.makeText(MasterChooser.this, "Empty URI not allowed.", Toast.LENGTH_SHORT).show();
00103     }
00104     // Make sure the URI can be parsed correctly.
00105     try {
00106       new URI(userUri);
00107     } catch (URISyntaxException e) {
00108       Toast.makeText(MasterChooser.this, "Invalid URI.", Toast.LENGTH_SHORT).show();
00109       return;
00110     }
00111 
00112     // If the displayed URI is valid then pack that into the intent.
00113     masterUri = userUri;
00114     SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
00115     editor.putString(PREFS_KEY_NAME, masterUri);
00116     editor.commit();
00117     // Package the intent to be consumed by the calling activity.
00118     Intent intent = new Intent();
00119     intent.putExtra("NEW_MASTER", false);
00120     intent.putExtra("ROS_MASTER_URI", masterUri);
00121     setResult(RESULT_OK, intent);
00122     finish();
00123   }
00124 
00125   public void qrCodeButtonClicked(View unused) {
00126     Intent intent = new Intent(BAR_CODE_SCANNER_PACKAGE_NAME);
00127     intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
00128     // Check if the Barcode Scanner is installed.
00129     if (!isQRCodeReaderInstalled(intent)) {
00130       // Open the Market and take them to the page from which they can download
00131       // the Barcode Scanner app.
00132       startActivity(new Intent(Intent.ACTION_VIEW,
00133           Uri.parse("market://details?id=com.google.zxing.client.android")));
00134     } else {
00135       // Call the Barcode Scanner to let the user scan a QR code.
00136       startActivityForResult(intent, 0);
00137     }
00138   }
00139 
00140   public void advancedCheckboxClicked(View view) {
00141     boolean checked = ((CheckBox) view).isChecked();
00142     Button new_public_master = (Button) findViewById(R.id.master_chooser_new_master_button);
00143     Button new_private_master = (Button) findViewById(R.id.master_chooser_new_private_master_button);
00144     if (checked) {
00145       new_private_master.setVisibility(View.VISIBLE);
00146       new_public_master.setVisibility(View.VISIBLE);
00147     } else {
00148       new_private_master.setVisibility(View.GONE);
00149       new_public_master.setVisibility(View.GONE);
00150     }
00151   }
00152 
00153   public Intent createNewMasterIntent (Boolean isPrivate) {
00154     Intent intent = new Intent();
00155     intent.putExtra("NEW_MASTER", true);
00156     intent.putExtra("ROS_MASTER_PRIVATE", isPrivate);
00157     return intent;
00158   }
00159 
00160   public void newMasterButtonClicked(View unused) {
00161     setResult(RESULT_OK, createNewMasterIntent(false));
00162     finish();
00163   }
00164 
00165   public void newPrivateMasterButtonClicked(View unused) {
00166     setResult(RESULT_OK, createNewMasterIntent(true));
00167     finish();
00168   }
00169 
00170   public void cancelButtonClicked(View unused) {
00171     setResult(RESULT_CANCELED);
00172     finish();
00173   }
00174 
00183   private boolean isQRCodeReaderInstalled(Intent intent) {
00184     List<ResolveInfo> list =
00185         getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
00186     return (list.size() > 0);
00187   }
00188 }


android_core
Author(s): Damon Kohler
autogenerated on Thu Aug 27 2015 12:11:33