Android Open Source - ScalAR Check File Manager Activity






From Project

Back to project page ScalAR.

License

The source code is released under:

GNU General Public License

If you think the Android project ScalAR listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
  Copyright (C) 2009  Tobias Domhan/*from   w  ww  .  ja v  a 2s  .c  o  m*/

    This file is part of AndObjViewer.

    AndObjViewer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    AndObjViewer is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with AndObjViewer.  If not, see <http://www.gnu.org/licenses/>.
 
 */
package edu.dhbw.andobjviewer;

import java.io.File;
import java.net.URI;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import edu.dhbw.andarmodelviewer.R;

/**
 * MainActivity for AndObjViewer. Let's the user pick a file and
 * hands over control to the model viewer.
 * @author Tobias Domhan
 *
 */
public class CheckFileManagerActivity extends Activity {
  /**
   * requestCode for file picking.
   */
  private final int PICK_FILE = 1;
  private final int VIEW_MODEL = 2;
  public static final int RESULT_ERROR = 3;
  
  /*
   * Dialogs:
   */
  private final int INSTALL_INTENT_DIALOG=1;
  
  private PackageManager packageManager;
  private Resources res;
  private TextView infoText;
  
  /**
   * Constants:
   */
  private final int TOAST_TIMEOUT = 3;
  
    /** Called when the activity is first created.
     * @param savedInstanceState saved instance state
     */
    @Override
    final public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.main);
        Context context = this;
        packageManager= context.getPackageManager();
        res = this.getResources();
        infoText = (TextView) findViewById(R.id.InfoText);        
        if (isPickFileIntentAvailable()) {
          selectFile();
        } else {
          installPickFileIntent();
        }
    }
    
    /* application resumed...
     * @see android.app.Activity#onResume()
     */
    @Override
    protected void onResume() {
      super.onResume();
    }
    
    
    /** Handling the select file here. 
     * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      switch (requestCode) {
        default:
        case PICK_FILE:
          switch(resultCode) {
            case Activity.RESULT_OK:
              //does file exist??
              File file =  new File(URI.create(data.getDataString()));
              if (!file.exists()) {
                //notify user that this file doesn't exist
                Toast.makeText(this, res.getText(R.string.file_doesnt_exist), TOAST_TIMEOUT).show();
                selectFile();
              } else {
                String fileName = data.getDataString();
                if(!fileName.endsWith(".obj")) {
                  Toast.makeText(this, res.getText(R.string.wrong_file), TOAST_TIMEOUT).show();
                  selectFile();
                } else {
                  //hand over control to the model viewer
                  Intent intent = new Intent(CheckFileManagerActivity.this, AugmentedModelViewerActivity.class);
                      intent.putExtra("name", data.getDataString());
                      intent.putExtra("type", AugmentedModelViewerActivity.TYPE_EXTERNAL);
                      intent.setAction(Intent.ACTION_VIEW);
                      startActivityForResult(intent, VIEW_MODEL);
                }
              }
              break;
            default:
            case Activity.RESULT_CANCELED:
              //back to the main activity
              Intent intent = new Intent(CheckFileManagerActivity.this, ModelChooser.class);
                  startActivity(intent);
              break;
          }
          break;
        case VIEW_MODEL:
          switch(resultCode) {
            case Activity.RESULT_OK:
              //model viewer returned...let the user view a new file
              selectFile();
              break;
            case Activity.RESULT_CANCELED:
              selectFile();
              break;
            case RESULT_ERROR:
              //something went wrong ... notify the user
              if(data != null) {
                Bundle extras = data.getExtras();
                String errorMessage = extras.getString("error_message");
                if(errorMessage != null)
                  Toast.makeText(this, extras.getString("error_message"), TOAST_TIMEOUT).show();
              }
              selectFile();
              break;  
          }
      }
    }
    
    
    
    /** Let the user select a File. The selected file will be handled in 
     *  {@link edu.dhbw.andobjviewer.CheckFileManagerActivity#onActivityResult(int, int, Intent)} */
    private void selectFile() {      
      //let the user select a model file
        Intent intent = new Intent("org.openintents.action.PICK_FILE");
        intent.setData(Uri.parse("file:///sdcard/"));
        intent.putExtra("org.openintents.extra.TITLE", res.getText(
            R.string.custom_model));
        startActivityForResult(intent, PICK_FILE);
    }
    
    
    /**
     * Indicates whether the specified action can be used as an intent. This
     * method queries the package manager for installed packages that can
     * respond to an intent with the specified action. If no suitable package is
     * found, this method returns false.
     *
     * @return True if an Intent with the specified action can be sent and
     * responded to, false otherwise.
     */
    private boolean isPickFileIntentAvailable() {
      return packageManager.queryIntentActivities(
          new Intent("org.openintents.action.PICK_FILE"), 0).size() > 0;
    }
    
    /**
     * Open's the market, so that the user can install the Intent needed for
     * file picking
     * @return boolean indicates if the android market is present on the phone
     */
    private boolean installPickFileIntent() {
      Uri marketUri = Uri.parse("market://search?q=pname:org.openintents.filemanager");
        Intent marketIntent = new Intent(Intent.ACTION_VIEW).setData(marketUri);
        if (!(packageManager
            .queryIntentActivities(marketIntent, 0).size() > 0)) {
          //no Market available
          //show info to user and exit 
          infoText.setText(res.getText(R.string.android_markt_not_avail));
          return false;
        } else {
          //notify user and start Android market            
            
            showDialog(INSTALL_INTENT_DIALOG);  
          return true;
        }        
    }
    
    /* (non-Javadoc)
     * @see android.app.Activity#onCreateDialog(int)
     */
    @Override
    protected Dialog onCreateDialog(int id) {
      Dialog dialog = null;
      switch(id){
        case INSTALL_INTENT_DIALOG:
          AlertDialog alertDialog = new AlertDialog.Builder(this).create();
              alertDialog.setMessage(res.getText(R.string.pickfile_intent_required));
              alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                 //launch android market
                 Uri marketUri = Uri.parse("market://search?q=pname:org.openintents.filemanager");
                   Intent marketIntent = new Intent(Intent.ACTION_VIEW).setData(marketUri);
                 startActivity(marketIntent);
                  return;
                } });  
              dialog = alertDialog;
          break;
      }
      return dialog;
    }
    
}




Java Source Code List

com.skylion.speech.GoogleTranslate.java
com.skylion.speech.Narrator.java
com.skylion.speech.Synthesiser.java
edu.dhbw.andar.ARObject.java
edu.dhbw.andar.ARToolkit.java
edu.dhbw.andar.AndARActivity.java
edu.dhbw.andar.AndARRenderer.java
edu.dhbw.andar.CameraHolder.java
edu.dhbw.andar.CameraParameters.java
edu.dhbw.andar.CameraPreviewHandler.java
edu.dhbw.andar.CameraStatus.java
edu.dhbw.andar.Config.java
edu.dhbw.andar.GenericFunctions.java
edu.dhbw.andar.exceptions.AndARException.java
edu.dhbw.andar.exceptions.AndARRuntimeException.java
edu.dhbw.andar.interfaces.MarkerVisibilityListener.java
edu.dhbw.andar.interfaces.OpenGLRenderer.java
edu.dhbw.andar.interfaces.PreviewFrameSink.java
edu.dhbw.andar.pub.CustomActivity.java
edu.dhbw.andar.pub.CustomObject.java
edu.dhbw.andar.pub.CustomRenderer.java
edu.dhbw.andar.pub.SimpleBox.java
edu.dhbw.andar.util.GraphicsUtil.java
edu.dhbw.andar.util.IO.java
edu.dhbw.andobjviewer.AugmentedModelViewerActivity.java
edu.dhbw.andobjviewer.CheckFileManagerActivity.java
edu.dhbw.andobjviewer.Config.java
edu.dhbw.andobjviewer.InstructionsActivity.java
edu.dhbw.andobjviewer.ModelChooser.java
edu.dhbw.andobjviewer.graphics.LightingRenderer.java
edu.dhbw.andobjviewer.graphics.Model3D.java
edu.dhbw.andobjviewer.graphics.Renderer.java
edu.dhbw.andobjviewer.models.Group.java
edu.dhbw.andobjviewer.models.Material.java
edu.dhbw.andobjviewer.models.Model.java
edu.dhbw.andobjviewer.models.Vector3D.java
edu.dhbw.andobjviewer.parser.MtlParser.java
edu.dhbw.andobjviewer.parser.ObjParser.java
edu.dhbw.andobjviewer.parser.ParseException.java
edu.dhbw.andobjviewer.parser.SimpleTokenizer.java
edu.dhbw.andobjviewer.parser.Util.java
edu.dhbw.andobjviewer.util.ArrayIterator.java
edu.dhbw.andobjviewer.util.AssetsFileUtil.java
edu.dhbw.andobjviewer.util.BaseFileUtil.java
edu.dhbw.andobjviewer.util.FixedPointUtils.java
edu.dhbw.andobjviewer.util.MatrixUtils.java
edu.dhbw.andobjviewer.util.MemUtil.java
edu.dhbw.andobjviewer.util.SDCardFileUtil.java