1 /*
2  * Copyright (C) 2015 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 
17 package com.example.android.rs.vr;
18 
19 import android.app.Activity;
20 import android.app.ProgressDialog;
21 import android.content.pm.ActivityInfo;
22 import android.content.res.Configuration;
23 import android.os.AsyncTask;
24 import android.os.Bundle;
25 import android.renderscript.RenderScript;
26 import android.util.Log;
27 import android.view.Menu;
28 import android.view.MenuItem;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.PopupMenu;
32 import android.widget.TextView;
33 import android.widget.ToggleButton;
34 
35 import java.io.File;
36 
37 import rsexample.google.com.vrdemo.R;
38 
39 import com.example.android.rs.vr.engine.Volume;
40 import com.example.android.rs.vr.engine.VrState;
41 import com.example.android.rs.vr.loaders.Droid;
42 import com.example.android.rs.vr.loaders.Mandelbulb;
43 import com.example.android.rs.vr.loaders.VolumeLoader;
44 
45 /**
46  * basic activity loads the volume and sets it on the VrView
47  */
48 public class VrActivity extends Activity {
49     private static final String LOGTAG = "VrActivity";
50     VrState mState = new VrState();
51     VrView mVrView;
52     VolumeLoader mLoader;
53     private RenderScript mRs;
54 
55     @Override
onCreate(Bundle savedInstanceState)56     protected void onCreate(Bundle savedInstanceState) {
57         super.onCreate(savedInstanceState);
58         setContentView(R.layout.activity_vr);
59 
60         mVrView = (VrView) findViewById(R.id.view);
61         mRs = RenderScript.create(VrActivity.this);
62 
63         String dir = "/sdcard/Download/volumes";
64         mLoader = new VolumeLoader(dir);
65         VrSetupTask setup = new VrSetupTask();
66         String [] names = mLoader.getNames();
67         setup.execute(names[0]);
68         TextView tv = (TextView) findViewById(R.id.title);
69         tv.setText(names[0]);
70     }
71 
72     class VrSetupTask extends AsyncTask<String, Integer, Volume> {
73         ProgressDialog progressDialog;
74         String message;
onPreExecute()75         protected void onPreExecute() {
76             super.onPreExecute();
77             progressDialog = new ProgressDialog(VrActivity.this);
78             progressDialog.setMessage(message= "Loading Volume");
79             progressDialog.setIndeterminate(true);
80             progressDialog.setCancelable(false);
81             progressDialog.setProgress(0);
82             progressDialog.setMax(100);
83             progressDialog.show();
84 
85             mLoader.setProgressListener(new VolumeLoader.ProgressListener() {
86                 @Override
87                 public void progress(int n, int total) {
88                      publishProgress(n, total);
89                 }
90             });
91         }
92 
93         @Override
doInBackground(String... names)94         protected Volume doInBackground(String... names) {
95             if (names[0].equals(Droid.NAME) || names[0].equals(Mandelbulb.NAME)) {
96                 message = "Computing "+names[0]+": ";
97             } else {
98                 message =" Loading " + names[0]+": ";
99             }
100             return  mLoader.getVolume(mRs, names[0]);
101         }
102 
103         @Override
onProgressUpdate(Integer... progress)104         protected void onProgressUpdate(Integer... progress) {
105             progressDialog.setMessage(message+progress[0]+"/"+progress[1]);
106             progressDialog.setMax(progress[1]);
107             progressDialog.setProgress(progress[0]);
108             Log.v(LOGTAG,"Loading "+ progress[0]+"/"+progress[1]);
109         }
110 
onPostExecute(Volume v)111         protected void onPostExecute(Volume v) {
112             Log.v(LOGTAG,"done");
113             mVrView.setVolume(mRs, v);
114             progressDialog.dismiss();
115             if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
116                 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
117             } else {
118                 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
119             }
120         }
121     }
122 
cutXClick(View v)123     public void cutXClick(View v) {
124         mVrView.setMode(VrView.CUT_X_MODE);
125         uncheckOthers(v);
126     }
127 
cutYClick(View v)128     public void cutYClick(View v) {
129         mVrView.setMode(VrView.CUT_Y_MODE);
130         uncheckOthers(v);
131     }
132 
cutZClick(View v)133     public void cutZClick(View v) {
134         mVrView.setMode(VrView.CUT_Z_MODE);
135         uncheckOthers(v);
136     }
137 
rotateClick(View v)138     public void rotateClick(View v) {
139         mVrView.setMode(VrView.ROTATE_MODE);
140         uncheckOthers(v);
141     }
142 
resetClick(View v)143     public void resetClick(View v) {
144         mVrView.resetCut();
145     }
146 
saveClick(View v)147     public void saveClick(View v) {
148         // TODO should save and Image
149     }
150 
looksClick(View v)151     public void looksClick(View v) {
152         String[] looks = mVrView.getLooks();
153         PopupMenu popup = new PopupMenu(this, v);
154         Menu menu = popup.getMenu();
155 
156         for (int i = 0; i < looks.length; i++) {
157             menu.add(0, Menu.FIRST + i, Menu.NONE, looks[i]);
158         }
159 
160         //registering popup with OnMenuItemClickListener
161         popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
162             public boolean onMenuItemClick(MenuItem item) {
163                 mVrView.setLook(item.getTitle().toString());
164                 return true;
165             }
166         });
167         popup.show();
168         uncheckOthers(v);
169     }
170 
dataClick(View v)171     public void dataClick(View v) {
172         Log.v(LOGTAG, "dataClick");
173 
174         String[] volumes = mLoader.getNames();
175         PopupMenu popup = new PopupMenu(this, v);
176         Menu menu = popup.getMenu();
177 
178         for (int i = 0; i < volumes.length; i++) {
179             menu.add(0, Menu.FIRST + i, Menu.NONE, volumes[i]);
180         }
181 
182         //registering popup with OnMenuItemClickListener
183         popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
184             public boolean onMenuItemClick(MenuItem item) {
185 
186                 VrSetupTask setup = new VrSetupTask();
187                 String title = item.getTitle().toString();
188                 TextView tv = (TextView) findViewById(R.id.title);
189                 tv.setText(title);
190                 setup.execute(title);
191                 return true;
192             }
193         });
194 
195         popup.show();
196         uncheckOthers(v);
197     }
198 
uncheckOthers(View v)199     private void uncheckOthers(View v) {
200         ViewGroup p = (ViewGroup) v.getParent().getParent();
201         uncheckOthers(p, v);
202     }
203 
uncheckOthers(ViewGroup p, View v)204     private void uncheckOthers(ViewGroup p, View v) {
205         int n = p.getChildCount();
206         for (int i = 0; i < n; i++) {
207             final View child = p.getChildAt(i);
208             if (child instanceof ViewGroup) {
209                 uncheckOthers((ViewGroup) child, v);
210             }
211             if (v != child && child instanceof ToggleButton) {
212                 ToggleButton t = (ToggleButton) child;
213                 t.setChecked(false);
214 
215             }
216         }
217     }
218 }
219