VrActivity.java revision 5b539461dcc159bd89297443780d635ccc5e3564
15b539461dcc159bd89297443780d635ccc5e3564John Hoford/*
25b539461dcc159bd89297443780d635ccc5e3564John Hoford * Copyright (C) 2015 The Android Open Source Project
35b539461dcc159bd89297443780d635ccc5e3564John Hoford *
45b539461dcc159bd89297443780d635ccc5e3564John Hoford * Licensed under the Apache License, Version 2.0 (the "License");
55b539461dcc159bd89297443780d635ccc5e3564John Hoford * you may not use this file except in compliance with the License.
65b539461dcc159bd89297443780d635ccc5e3564John Hoford * You may obtain a copy of the License at
75b539461dcc159bd89297443780d635ccc5e3564John Hoford *
85b539461dcc159bd89297443780d635ccc5e3564John Hoford *      http://www.apache.org/licenses/LICENSE-2.0
95b539461dcc159bd89297443780d635ccc5e3564John Hoford *
105b539461dcc159bd89297443780d635ccc5e3564John Hoford * Unless required by applicable law or agreed to in writing, software
115b539461dcc159bd89297443780d635ccc5e3564John Hoford * distributed under the License is distributed on an "AS IS" BASIS,
125b539461dcc159bd89297443780d635ccc5e3564John Hoford * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135b539461dcc159bd89297443780d635ccc5e3564John Hoford * See the License for the specific language governing permissions and
145b539461dcc159bd89297443780d635ccc5e3564John Hoford * limitations under the License.
155b539461dcc159bd89297443780d635ccc5e3564John Hoford */
165b539461dcc159bd89297443780d635ccc5e3564John Hoford
175b539461dcc159bd89297443780d635ccc5e3564John Hofordpackage com.example.android.rs.vr;
185b539461dcc159bd89297443780d635ccc5e3564John Hoford
195b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.app.Activity;
205b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.app.ProgressDialog;
215b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.os.AsyncTask;
225b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.os.Bundle;
235b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.renderscript.RenderScript;
245b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.util.Log;
255b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.view.Menu;
265b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.view.MenuItem;
275b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.view.View;
285b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.view.ViewGroup;
295b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.widget.PopupMenu;
305b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.widget.TextView;
315b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.widget.ToggleButton;
325b539461dcc159bd89297443780d635ccc5e3564John Hoford
335b539461dcc159bd89297443780d635ccc5e3564John Hofordimport java.io.File;
345b539461dcc159bd89297443780d635ccc5e3564John Hoford
355b539461dcc159bd89297443780d635ccc5e3564John Hofordimport rsexample.google.com.vrdemo.R;
365b539461dcc159bd89297443780d635ccc5e3564John Hoford
375b539461dcc159bd89297443780d635ccc5e3564John Hofordimport com.example.android.rs.vr.engine.Volume;
385b539461dcc159bd89297443780d635ccc5e3564John Hofordimport com.example.android.rs.vr.engine.VrState;
395b539461dcc159bd89297443780d635ccc5e3564John Hofordimport com.example.android.rs.vr.loaders.VolumeLoader;
405b539461dcc159bd89297443780d635ccc5e3564John Hoford
415b539461dcc159bd89297443780d635ccc5e3564John Hoford/**
425b539461dcc159bd89297443780d635ccc5e3564John Hoford * basic activity loads the volume and sets it on the VrView
435b539461dcc159bd89297443780d635ccc5e3564John Hoford */
445b539461dcc159bd89297443780d635ccc5e3564John Hofordpublic class VrActivity extends Activity {
455b539461dcc159bd89297443780d635ccc5e3564John Hoford    private static final String LOGTAG = "VrActivity";
465b539461dcc159bd89297443780d635ccc5e3564John Hoford    VrState mState = new VrState();
475b539461dcc159bd89297443780d635ccc5e3564John Hoford    VrView mVrView;
485b539461dcc159bd89297443780d635ccc5e3564John Hoford    VolumeLoader mLoader;
495b539461dcc159bd89297443780d635ccc5e3564John Hoford    private RenderScript mRs;
505b539461dcc159bd89297443780d635ccc5e3564John Hoford
515b539461dcc159bd89297443780d635ccc5e3564John Hoford    @Override
525b539461dcc159bd89297443780d635ccc5e3564John Hoford    protected void onCreate(Bundle savedInstanceState) {
535b539461dcc159bd89297443780d635ccc5e3564John Hoford        super.onCreate(savedInstanceState);
545b539461dcc159bd89297443780d635ccc5e3564John Hoford        setContentView(R.layout.activity_vr);
555b539461dcc159bd89297443780d635ccc5e3564John Hoford
565b539461dcc159bd89297443780d635ccc5e3564John Hoford        mVrView = (VrView) findViewById(R.id.view);
575b539461dcc159bd89297443780d635ccc5e3564John Hoford        mRs = RenderScript.create(VrActivity.this);
585b539461dcc159bd89297443780d635ccc5e3564John Hoford
595b539461dcc159bd89297443780d635ccc5e3564John Hoford        String dir = "/sdcard/Download/volumes";
605b539461dcc159bd89297443780d635ccc5e3564John Hoford        mLoader = new VolumeLoader(dir);
615b539461dcc159bd89297443780d635ccc5e3564John Hoford        VrSetupTask setup = new VrSetupTask();
625b539461dcc159bd89297443780d635ccc5e3564John Hoford        String [] names = mLoader.getNames();
635b539461dcc159bd89297443780d635ccc5e3564John Hoford        setup.execute(names[0]);
645b539461dcc159bd89297443780d635ccc5e3564John Hoford        TextView tv = (TextView) findViewById(R.id.title);
655b539461dcc159bd89297443780d635ccc5e3564John Hoford        tv.setText(names[0]);
665b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
675b539461dcc159bd89297443780d635ccc5e3564John Hoford
685b539461dcc159bd89297443780d635ccc5e3564John Hoford    class VrSetupTask extends AsyncTask<String, Integer, Volume> {
695b539461dcc159bd89297443780d635ccc5e3564John Hoford        ProgressDialog progressDialog;
705b539461dcc159bd89297443780d635ccc5e3564John Hoford
715b539461dcc159bd89297443780d635ccc5e3564John Hoford        protected void onPreExecute() {
725b539461dcc159bd89297443780d635ccc5e3564John Hoford            super.onPreExecute();
735b539461dcc159bd89297443780d635ccc5e3564John Hoford            progressDialog = new ProgressDialog(VrActivity.this);
745b539461dcc159bd89297443780d635ccc5e3564John Hoford            progressDialog.setMessage( "Loading Volume");
755b539461dcc159bd89297443780d635ccc5e3564John Hoford            progressDialog.setIndeterminate(true);
765b539461dcc159bd89297443780d635ccc5e3564John Hoford            progressDialog.setCancelable(false);
775b539461dcc159bd89297443780d635ccc5e3564John Hoford            progressDialog.setProgress(0);
785b539461dcc159bd89297443780d635ccc5e3564John Hoford            progressDialog.setMax(100);
795b539461dcc159bd89297443780d635ccc5e3564John Hoford            progressDialog.show();
805b539461dcc159bd89297443780d635ccc5e3564John Hoford
815b539461dcc159bd89297443780d635ccc5e3564John Hoford            mLoader.setProgressListener(new VolumeLoader.ProgressListener() {
825b539461dcc159bd89297443780d635ccc5e3564John Hoford                @Override
835b539461dcc159bd89297443780d635ccc5e3564John Hoford                public void progress(int n, int total) {
845b539461dcc159bd89297443780d635ccc5e3564John Hoford                     publishProgress(n, total);
855b539461dcc159bd89297443780d635ccc5e3564John Hoford                }
865b539461dcc159bd89297443780d635ccc5e3564John Hoford            });
875b539461dcc159bd89297443780d635ccc5e3564John Hoford        }
885b539461dcc159bd89297443780d635ccc5e3564John Hoford
895b539461dcc159bd89297443780d635ccc5e3564John Hoford        @Override
905b539461dcc159bd89297443780d635ccc5e3564John Hoford        protected Volume doInBackground(String... names) {
915b539461dcc159bd89297443780d635ccc5e3564John Hoford            return  mLoader.getVolume(mRs, names[0]);
925b539461dcc159bd89297443780d635ccc5e3564John Hoford        }
935b539461dcc159bd89297443780d635ccc5e3564John Hoford
945b539461dcc159bd89297443780d635ccc5e3564John Hoford        @Override
955b539461dcc159bd89297443780d635ccc5e3564John Hoford        protected void onProgressUpdate(Integer... progress) {
965b539461dcc159bd89297443780d635ccc5e3564John Hoford            progressDialog.setMessage("load "+progress[0]+"/"+progress[1]);
975b539461dcc159bd89297443780d635ccc5e3564John Hoford            progressDialog.setMax(progress[1]);
985b539461dcc159bd89297443780d635ccc5e3564John Hoford            progressDialog.setProgress(progress[0]);
995b539461dcc159bd89297443780d635ccc5e3564John Hoford            Log.v(LOGTAG,"Loading "+ progress[0]+"/"+progress[1]);
1005b539461dcc159bd89297443780d635ccc5e3564John Hoford        }
1015b539461dcc159bd89297443780d635ccc5e3564John Hoford
1025b539461dcc159bd89297443780d635ccc5e3564John Hoford        protected void onPostExecute(Volume v) {
1035b539461dcc159bd89297443780d635ccc5e3564John Hoford            Log.v(LOGTAG,"done");
1045b539461dcc159bd89297443780d635ccc5e3564John Hoford            mVrView.setVolume(mRs, v);
1055b539461dcc159bd89297443780d635ccc5e3564John Hoford            progressDialog.dismiss();
1065b539461dcc159bd89297443780d635ccc5e3564John Hoford        }
1075b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
1085b539461dcc159bd89297443780d635ccc5e3564John Hoford
1095b539461dcc159bd89297443780d635ccc5e3564John Hoford    public void cutXClick(View v) {
1105b539461dcc159bd89297443780d635ccc5e3564John Hoford        mVrView.setMode(VrView.CUT_X_MODE);
1115b539461dcc159bd89297443780d635ccc5e3564John Hoford        uncheckOthers(v);
1125b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
1135b539461dcc159bd89297443780d635ccc5e3564John Hoford
1145b539461dcc159bd89297443780d635ccc5e3564John Hoford    public void cutYClick(View v) {
1155b539461dcc159bd89297443780d635ccc5e3564John Hoford        mVrView.setMode(VrView.CUT_Y_MODE);
1165b539461dcc159bd89297443780d635ccc5e3564John Hoford        uncheckOthers(v);
1175b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
1185b539461dcc159bd89297443780d635ccc5e3564John Hoford
1195b539461dcc159bd89297443780d635ccc5e3564John Hoford    public void cutZClick(View v) {
1205b539461dcc159bd89297443780d635ccc5e3564John Hoford        mVrView.setMode(VrView.CUT_Z_MODE);
1215b539461dcc159bd89297443780d635ccc5e3564John Hoford        uncheckOthers(v);
1225b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
1235b539461dcc159bd89297443780d635ccc5e3564John Hoford
1245b539461dcc159bd89297443780d635ccc5e3564John Hoford    public void rotateClick(View v) {
1255b539461dcc159bd89297443780d635ccc5e3564John Hoford        mVrView.setMode(VrView.ROTATE_MODE);
1265b539461dcc159bd89297443780d635ccc5e3564John Hoford        uncheckOthers(v);
1275b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
1285b539461dcc159bd89297443780d635ccc5e3564John Hoford
1295b539461dcc159bd89297443780d635ccc5e3564John Hoford    public void resetClick(View v) {
1305b539461dcc159bd89297443780d635ccc5e3564John Hoford        mVrView.resetCut();
1315b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
1325b539461dcc159bd89297443780d635ccc5e3564John Hoford
1335b539461dcc159bd89297443780d635ccc5e3564John Hoford    public void saveClick(View v) {
1345b539461dcc159bd89297443780d635ccc5e3564John Hoford        // TODO should save and Image
1355b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
1365b539461dcc159bd89297443780d635ccc5e3564John Hoford
1375b539461dcc159bd89297443780d635ccc5e3564John Hoford    public void looksClick(View v) {
1385b539461dcc159bd89297443780d635ccc5e3564John Hoford        String[] looks = mVrView.getLooks();
1395b539461dcc159bd89297443780d635ccc5e3564John Hoford        PopupMenu popup = new PopupMenu(this, v);
1405b539461dcc159bd89297443780d635ccc5e3564John Hoford        Menu menu = popup.getMenu();
1415b539461dcc159bd89297443780d635ccc5e3564John Hoford
1425b539461dcc159bd89297443780d635ccc5e3564John Hoford        for (int i = 0; i < looks.length; i++) {
1435b539461dcc159bd89297443780d635ccc5e3564John Hoford            menu.add(0, Menu.FIRST + i, Menu.NONE, looks[i]);
1445b539461dcc159bd89297443780d635ccc5e3564John Hoford        }
1455b539461dcc159bd89297443780d635ccc5e3564John Hoford
1465b539461dcc159bd89297443780d635ccc5e3564John Hoford        //registering popup with OnMenuItemClickListener
1475b539461dcc159bd89297443780d635ccc5e3564John Hoford        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
1485b539461dcc159bd89297443780d635ccc5e3564John Hoford            public boolean onMenuItemClick(MenuItem item) {
1495b539461dcc159bd89297443780d635ccc5e3564John Hoford                mVrView.setLook(item.getTitle().toString());
1505b539461dcc159bd89297443780d635ccc5e3564John Hoford                return true;
1515b539461dcc159bd89297443780d635ccc5e3564John Hoford            }
1525b539461dcc159bd89297443780d635ccc5e3564John Hoford        });
1535b539461dcc159bd89297443780d635ccc5e3564John Hoford        popup.show();
1545b539461dcc159bd89297443780d635ccc5e3564John Hoford        uncheckOthers(v);
1555b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
1565b539461dcc159bd89297443780d635ccc5e3564John Hoford
1575b539461dcc159bd89297443780d635ccc5e3564John Hoford    public void dataClick(View v) {
1585b539461dcc159bd89297443780d635ccc5e3564John Hoford        Log.v(LOGTAG, "dataClick");
1595b539461dcc159bd89297443780d635ccc5e3564John Hoford
1605b539461dcc159bd89297443780d635ccc5e3564John Hoford        String[] volumes = mLoader.getNames();
1615b539461dcc159bd89297443780d635ccc5e3564John Hoford        PopupMenu popup = new PopupMenu(this, v);
1625b539461dcc159bd89297443780d635ccc5e3564John Hoford        Menu menu = popup.getMenu();
1635b539461dcc159bd89297443780d635ccc5e3564John Hoford
1645b539461dcc159bd89297443780d635ccc5e3564John Hoford        for (int i = 0; i < volumes.length; i++) {
1655b539461dcc159bd89297443780d635ccc5e3564John Hoford            menu.add(0, Menu.FIRST + i, Menu.NONE, volumes[i]);
1665b539461dcc159bd89297443780d635ccc5e3564John Hoford        }
1675b539461dcc159bd89297443780d635ccc5e3564John Hoford
1685b539461dcc159bd89297443780d635ccc5e3564John Hoford        //registering popup with OnMenuItemClickListener
1695b539461dcc159bd89297443780d635ccc5e3564John Hoford        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
1705b539461dcc159bd89297443780d635ccc5e3564John Hoford            public boolean onMenuItemClick(MenuItem item) {
1715b539461dcc159bd89297443780d635ccc5e3564John Hoford
1725b539461dcc159bd89297443780d635ccc5e3564John Hoford                VrSetupTask setup = new VrSetupTask();
1735b539461dcc159bd89297443780d635ccc5e3564John Hoford                String title = item.getTitle().toString();
1745b539461dcc159bd89297443780d635ccc5e3564John Hoford                TextView tv = (TextView) findViewById(R.id.title);
1755b539461dcc159bd89297443780d635ccc5e3564John Hoford                tv.setText(title);
1765b539461dcc159bd89297443780d635ccc5e3564John Hoford                setup.execute(title);
1775b539461dcc159bd89297443780d635ccc5e3564John Hoford                return true;
1785b539461dcc159bd89297443780d635ccc5e3564John Hoford            }
1795b539461dcc159bd89297443780d635ccc5e3564John Hoford        });
1805b539461dcc159bd89297443780d635ccc5e3564John Hoford
1815b539461dcc159bd89297443780d635ccc5e3564John Hoford        popup.show();
1825b539461dcc159bd89297443780d635ccc5e3564John Hoford        uncheckOthers(v);
1835b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
1845b539461dcc159bd89297443780d635ccc5e3564John Hoford
1855b539461dcc159bd89297443780d635ccc5e3564John Hoford    private void uncheckOthers(View v) {
1865b539461dcc159bd89297443780d635ccc5e3564John Hoford        ViewGroup p = (ViewGroup) v.getParent().getParent();
1875b539461dcc159bd89297443780d635ccc5e3564John Hoford        uncheckOthers(p, v);
1885b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
1895b539461dcc159bd89297443780d635ccc5e3564John Hoford
1905b539461dcc159bd89297443780d635ccc5e3564John Hoford    private void uncheckOthers(ViewGroup p, View v) {
1915b539461dcc159bd89297443780d635ccc5e3564John Hoford        int n = p.getChildCount();
1925b539461dcc159bd89297443780d635ccc5e3564John Hoford        for (int i = 0; i < n; i++) {
1935b539461dcc159bd89297443780d635ccc5e3564John Hoford            final View child = p.getChildAt(i);
1945b539461dcc159bd89297443780d635ccc5e3564John Hoford            if (child instanceof ViewGroup) {
1955b539461dcc159bd89297443780d635ccc5e3564John Hoford                uncheckOthers((ViewGroup) child, v);
1965b539461dcc159bd89297443780d635ccc5e3564John Hoford            }
1975b539461dcc159bd89297443780d635ccc5e3564John Hoford            if (v != child && child instanceof ToggleButton) {
1985b539461dcc159bd89297443780d635ccc5e3564John Hoford                ToggleButton t = (ToggleButton) child;
1995b539461dcc159bd89297443780d635ccc5e3564John Hoford                t.setChecked(false);
2005b539461dcc159bd89297443780d635ccc5e3564John Hoford
2015b539461dcc159bd89297443780d635ccc5e3564John Hoford            }
2025b539461dcc159bd89297443780d635ccc5e3564John Hoford        }
2035b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
2045b539461dcc159bd89297443780d635ccc5e3564John Hoford}
205