Ticket #1246: HostInfoNCpu.patch

File HostInfoNCpu.patch, 19.7 KB (added by big23boy, 11 years ago)

Host info #cpu changes

  • android/BOINC/res/values/strings.xml

    From c0a126f532b36737b4076c019bb6ba2a6168c591 Mon Sep 17 00:00:00 2001
    From: Keith Uplinger <uplink@us.ibm.com>
    Date: Tue, 21 May 2013 23:58:35 -0500
    Subject: [PATCH] These changes are for having the hostinfo being pulled for
     preferences to show cpu selector in quantity with max ncpus
     of the device.
    
    ---
     android/BOINC/res/values/strings.xml               |    2 +
     .../src/edu/berkeley/boinc/PrefsActivity.java      |  157 +++++++++++++++-----
     .../berkeley/boinc/adapter/PrefsListAdapter.java   |   18 ++-
     .../boinc/adapter/PrefsListItemWrapperDouble.java  |    6 -
     .../boinc/adapter/PrefsListItemWrapperInteger.java |   50 +++++++
     .../edu/berkeley/boinc/client/ClientStatus.java    |   13 +-
     .../src/edu/berkeley/boinc/client/Monitor.java     |    4 +-
     7 files changed, 202 insertions(+), 48 deletions(-)
     create mode 100644 android/BOINC/src/edu/berkeley/boinc/adapter/PrefsListItemWrapperInteger.java
    
    diff --git a/android/BOINC/res/values/strings.xml b/android/BOINC/res/values/strings.xml
    index f906ebe..feb2026 100644
    a b  
    167167        <string name="prefs_unit_mb">MB</string>
    168168        <string name="prefs_unit_gb">GB</string>
    169169        <string name="prefs_unit_pct">%</string>
     170        <string name="prefs_unit_cpu">CPU</string>
     171        <string name="prefs_unit_cpus">CPUS</string>
    170172       
    171173        <!-- projects tab strings -->
    172174        <string name="projects_loading">Reading projects&#8230;</string>
  • android/BOINC/src/edu/berkeley/boinc/PrefsActivity.java

    diff --git a/android/BOINC/src/edu/berkeley/boinc/PrefsActivity.java b/android/BOINC/src/edu/berkeley/boinc/PrefsActivity.java
    index f244225..caa9ee6 100644
    a b import edu.berkeley.boinc.adapter.PrefsListAdapter; 
    2424import edu.berkeley.boinc.adapter.PrefsListItemWrapper;
    2525import edu.berkeley.boinc.adapter.PrefsListItemWrapperBool;
    2626import edu.berkeley.boinc.adapter.PrefsListItemWrapperDouble;
     27import edu.berkeley.boinc.adapter.PrefsListItemWrapperInteger;
    2728import edu.berkeley.boinc.client.ClientNotification;
    2829import edu.berkeley.boinc.client.Monitor;
    2930import edu.berkeley.boinc.rpc.GlobalPreferences;
     31import edu.berkeley.boinc.rpc.HostInfo;
    3032import android.app.AlertDialog;
    3133import android.app.Dialog;
    3234import android.content.ComponentName;
    public class PrefsActivity extends FragmentActivity { 
    6062        private ArrayList<PrefsListItemWrapper> data = new ArrayList<PrefsListItemWrapper>(); //Adapter for list data
    6163        private GlobalPreferences clientPrefs = null; //preferences of the client, read on every onResume via RPC
    6264        private AppPreferences appPrefs = null; //Android specific preferences, singleton of monitor
     65        private HostInfo hostinfo = null;
    6366       
    6467        private Dialog dialog; //Dialog for input on non-Bool preferences
    6568        private PrefsListItemWrapperDouble dialogItem; // saves content of preference Dialog is showing
     69        private PrefsListItemWrapperInteger dialogItemInteger;
    6670       
    6771        public void onCreate(Bundle savedInstanceState) {
    6872                super.onCreate(savedInstanceState);
    public class PrefsActivity extends FragmentActivity { 
    112116                return true;
    113117        }
    114118       
     119        private Boolean getHostInfo() {
     120                hostinfo = Monitor.getClientStatus().getHostInfo(); //Get the hostinfo from client via rpc
     121                if(hostinfo == null) {
     122                        Log.d(TAG, "getHostInfo: null, return false");
     123                        return false;
     124                }
     125                return true;
     126        }
     127       
    115128        private void populateLayout() {
    116129               
    117                 if(!getPrefs() || appPrefs == null) {
     130                if(!getPrefs() || appPrefs == null || !getHostInfo()) {
    118131                        Log.d(TAG, "populateLayout returns, data is not present");
    119132                        setLayoutLoading();
    120133                        return;
    public class PrefsActivity extends FragmentActivity { 
    140153        data.add(new PrefsListItemWrapper(this,R.string.prefs_category_power,true));
    141154                data.add(new PrefsListItemWrapperBool(this,R.string.prefs_run_on_battery_header,R.string.prefs_category_power,clientPrefs.run_on_batteries));
    142155                if(advanced) data.add(new PrefsListItemWrapper(this,R.string.prefs_category_cpu,true));
    143                 if(advanced) data.add(new PrefsListItemWrapperDouble(this,R.string.prefs_cpu_number_cpus_header,R.string.prefs_category_cpu,clientPrefs.max_ncpus_pct));
     156                Integer ncpus = (int)((double)hostinfo.p_ncpus * (clientPrefs.max_ncpus_pct / 100.0));
     157                if(ncpus.equals(0))
     158                        ncpus = 1;
     159                if(advanced) data.add(new PrefsListItemWrapperInteger(this,R.string.prefs_cpu_number_cpus_header,R.string.prefs_category_cpu,ncpus));
    144160                if(advanced) data.add(new PrefsListItemWrapperDouble(this,R.string.prefs_cpu_time_max_header,R.string.prefs_category_cpu,clientPrefs.cpu_usage_limit));
    145161                if(advanced) data.add(new PrefsListItemWrapperDouble(this,R.string.prefs_cpu_other_load_suspension_header,R.string.prefs_category_cpu,clientPrefs.suspend_cpu_usage));
    146162                if(advanced) data.add(new PrefsListItemWrapper(this,R.string.prefs_category_storage,true));
    public class PrefsActivity extends FragmentActivity { 
    192208       
    193209        // onClick of listview items with PrefsListItemWrapperDouble
    194210        public void onItemClick (View view) {
    195                 PrefsListItemWrapperDouble listItem = (PrefsListItemWrapperDouble) view.getTag();
    196                 Log.d(TAG,"onItemClick " + listItem.ID);
     211                if(view.getTag() instanceof PrefsListItemWrapperDouble) {
     212                        PrefsListItemWrapperDouble listItem = (PrefsListItemWrapperDouble) view.getTag();
     213                        Log.d(TAG,"onItemClick Double " + listItem.ID);
    197214
    198                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
    199                 LayoutInflater inflater = getLayoutInflater();
    200                 final View dialogContent;
    201                 if(listItem.isPct) {
    202                         dialogContent = inflater.inflate(R.layout.prefs_layout_dialog_pct, null);
    203                         TextView sliderProgress = (TextView) dialogContent.findViewById(R.id.seekbar_status);
    204                         sliderProgress.setText(listItem.status.intValue() + " %");
    205                         SeekBar slider = (SeekBar) dialogContent.findViewById(R.id.seekbar);
    206                         slider.setProgress(listItem.status.intValue());
    207                         slider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    208                         public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
    209                                 String progressString = progress + " %";
    210                                 TextView sliderProgress = (TextView) dialogContent.findViewById(R.id.seekbar_status);
    211                             sliderProgress.setText(progressString);
    212                         }
    213                                 @Override
    214                                 public void onStartTrackingTouch(SeekBar seekBar) {}
    215                                 @Override
    216                                 public void onStopTrackingTouch(SeekBar seekBar) {}
    217                     });
    218                 } else {
    219                         dialogContent = inflater.inflate(R.layout.prefs_layout_dialog, null);
    220                 }
    221         builder.setMessage(listItem.ID)
    222                    .setView(dialogContent)
    223                .setNegativeButton(R.string.prefs_cancel_button, new DialogInterface.OnClickListener() {
    224                    public void onClick(DialogInterface dialogI, int id) {
    225                        dialog.cancel();
    226                    }
    227                })
    228                .setPositiveButton(R.string.prefs_submit_button, new DialogInterface.OnClickListener() {
     215                        AlertDialog.Builder builder = new AlertDialog.Builder(this);
     216                        LayoutInflater inflater = getLayoutInflater();
     217                        final View dialogContent;
     218                        if(listItem.isPct) {
     219                                dialogContent = inflater.inflate(R.layout.prefs_layout_dialog_pct, null);
     220                                TextView sliderProgress = (TextView) dialogContent.findViewById(R.id.seekbar_status);
     221                                sliderProgress.setText(listItem.status.intValue() + " %");
     222                                SeekBar slider = (SeekBar) dialogContent.findViewById(R.id.seekbar);
     223                                slider.setProgress(listItem.status.intValue());
     224                                slider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
     225                                        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
     226                                                String progressString = progress + " %";
     227                                                TextView sliderProgress = (TextView) dialogContent.findViewById(R.id.seekbar_status);
     228                                                sliderProgress.setText(progressString);
     229                                        }
     230                                        @Override
     231                                        public void onStartTrackingTouch(SeekBar seekBar) {}
     232                                        @Override
     233                                        public void onStopTrackingTouch(SeekBar seekBar) {}
     234                                });
     235                        } else {
     236                                dialogContent = inflater.inflate(R.layout.prefs_layout_dialog, null);
     237                        }
     238                        builder.setMessage(listItem.ID)
     239                        .setView(dialogContent)
     240                        .setNegativeButton(R.string.prefs_cancel_button, new DialogInterface.OnClickListener() {
     241                                public void onClick(DialogInterface dialogI, int id) {
     242                                        dialog.cancel();
     243                                        }
     244                                })
     245                        .setPositiveButton(R.string.prefs_submit_button, new DialogInterface.OnClickListener() {
    229246                   public void onClick(DialogInterface dialogI, int id) {
    230247                           double value;
    231248                           if(dialogItem.isPct) {
    public class PrefsActivity extends FragmentActivity { 
    241258                           writeDoublePreference(dialogItem.ID, value);
    242259                   }
    243260               });
    244         dialog = builder.create();
    245         dialog.show();
    246         dialogItem = listItem; // set dialog content
     261                        dialog = builder.create();
     262                dialog.show();
     263                dialogItem = listItem; // set dialog content
     264                } else if(view.getTag() instanceof PrefsListItemWrapperInteger) {
     265                        PrefsListItemWrapperInteger listItem = (PrefsListItemWrapperInteger) view.getTag();
     266                        Log.d(TAG,"onItemClick Integer " + listItem.ID);
     267                       
     268                        AlertDialog.Builder builder = new AlertDialog.Builder(this);
     269                        LayoutInflater inflater = getLayoutInflater();
     270                        final View dialogContent;
     271                        if(listItem.ID.equals(R.string.prefs_cpu_number_cpus_header)) {
     272                                if(!getHostInfo()) {
     273                                        Log.d(TAG, "onItemClick missing hostInfo");
     274                                        return;
     275                                }
     276                               
     277                                dialogContent = inflater.inflate(R.layout.prefs_layout_dialog_pct, null);
     278                                TextView sliderProgress = (TextView) dialogContent.findViewById(R.id.seekbar_status);
     279                                if(listItem.status > 1) {
     280                                        sliderProgress.setText(listItem.status + " CPUS");
     281                                } else {
     282                                        sliderProgress.setText(listItem.status + " CPU");
     283                                }
     284                                SeekBar slider = (SeekBar) dialogContent.findViewById(R.id.seekbar);
     285                                slider.setMax(hostinfo.p_ncpus);
     286                                slider.setProgress(listItem.status);
     287                                slider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
     288                                        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
     289                                                String progressString = String.valueOf(progress);
     290                                                TextView sliderProgress = (TextView) dialogContent.findViewById(R.id.seekbar_status);
     291                                                sliderProgress.setText(progressString);
     292                                        }
     293                                        @Override
     294                                        public void onStartTrackingTouch(SeekBar seekBar) {}
     295                                        @Override
     296                                        public void onStopTrackingTouch(SeekBar seekBar) {}
     297                                });
     298                        } else {
     299                                dialogContent = inflater.inflate(R.layout.prefs_layout_dialog, null);
     300                        }
     301                        builder.setMessage(listItem.ID)
     302                        .setView(dialogContent)
     303                        .setNegativeButton(R.string.prefs_cancel_button, new DialogInterface.OnClickListener() {
     304                                public void onClick(DialogInterface dialogI, int id) {
     305                                        dialog.cancel();
     306                                        }
     307                                })
     308                        .setPositiveButton(R.string.prefs_submit_button, new DialogInterface.OnClickListener() {
     309                   public void onClick(DialogInterface dialogI, int id) {
     310                           int value;
     311                           SeekBar slider = (SeekBar) dialog.findViewById(R.id.seekbar);
     312                           value = slider.getProgress();
     313                           writeIntegerAsDoublePreference(dialogItemInteger.ID, value, hostinfo.p_ncpus);
     314                   }
     315               });
     316                        dialog = builder.create();
     317                dialog.show();
     318                dialogItemInteger = listItem; // set dialog content
     319                }
    247320        }
    248321
    249322        @Override
    public class PrefsActivity extends FragmentActivity { 
    253326            doUnbindService();
    254327        }
    255328       
     329        private void writeIntegerAsDoublePreference(int id, Integer value, Integer max) {
     330                switch(id) {
     331                case R.string.prefs_cpu_number_cpus_header:
     332                        clientPrefs.max_ncpus_pct = value.doubleValue() / max.doubleValue();
     333                        break;
     334                }
     335        }
     336       
    256337        private void writeDoublePreference(int id, double value) {
    257338                // update preferences
    258339                switch (id) {
  • android/BOINC/src/edu/berkeley/boinc/adapter/PrefsListAdapter.java

    diff --git a/android/BOINC/src/edu/berkeley/boinc/adapter/PrefsListAdapter.java b/android/BOINC/src/edu/berkeley/boinc/adapter/PrefsListAdapter.java
    index 6d4316a..d4720b9 100644
    a b public class PrefsListAdapter extends ArrayAdapter<PrefsListItemWrapper>{ 
    7777                        }
    7878                        TextView status = (TextView) v.findViewById(R.id.status);
    7979                        status.setText(value + " " + item.unit);
    80                 }
     80                } else if(listItem instanceof PrefsListItemWrapperInteger) {
     81                        PrefsListItemWrapperInteger item = (PrefsListItemWrapperInteger) listItem;
     82                        v = vi.inflate(R.layout.prefs_layout_listitem, null);
     83                        v.setTag(listItem); //set listItem as tag to view, since root layout defines onClick method
     84                        TextView header = (TextView) v.findViewById(R.id.header);
     85                        header.setText(item.header);
     86                        TextView description = (TextView) v.findViewById(R.id.description);
     87                        description.setText(item.description.toString());
     88                       
     89                        String value = item.status.toString();
     90                       
     91                        TextView status = (TextView) v.findViewById(R.id.status);
     92                        if(!item.unit.isEmpty()) {
     93                                value = value + " " + item.unit;
     94                        }
     95                        status.setText(value);
     96                }
    8197        }
    8298       
    8399        return v;
  • android/BOINC/src/edu/berkeley/boinc/adapter/PrefsListItemWrapperDouble.java

    diff --git a/android/BOINC/src/edu/berkeley/boinc/adapter/PrefsListItemWrapperDouble.java b/android/BOINC/src/edu/berkeley/boinc/adapter/PrefsListItemWrapperDouble.java
    index d0cb2d5..a56c841 100644
    a b public class PrefsListItemWrapperDouble extends PrefsListItemWrapper { 
    5858                        unit = ctx.getString(R.string.prefs_unit_mb);
    5959                        isPct = false;
    6060                        break;
    61                 case R.string.prefs_cpu_number_cpus_header:
    62                         header = ctx.getString(R.string.prefs_cpu_number_cpus_header);
    63                         description = ctx.getString(R.string.prefs_cpu_number_cpus_description);
    64                         unit = ctx.getString(R.string.prefs_unit_pct);
    65                         isPct = true;
    66                         break;
    6761                case R.string.prefs_cpu_other_load_suspension_header:
    6862                        header = ctx.getString(R.string.prefs_cpu_other_load_suspension_header);
    6963                        description = ctx.getString(R.string.prefs_cpu_other_load_suspension_description);
  • new file android/BOINC/src/edu/berkeley/boinc/adapter/PrefsListItemWrapperInteger.java

    diff --git a/android/BOINC/src/edu/berkeley/boinc/adapter/PrefsListItemWrapperInteger.java b/android/BOINC/src/edu/berkeley/boinc/adapter/PrefsListItemWrapperInteger.java
    new file mode 100644
    index 0000000..36ffe18
    - +  
     1/*******************************************************************************
     2 * This file is part of BOINC.
     3 * http://boinc.berkeley.edu
     4 * Copyright (C) 2012 University of California
     5 *
     6 * BOINC is free software; you can redistribute it and/or modify it
     7 * under the terms of the GNU Lesser General Public License
     8 * as published by the Free Software Foundation,
     9 * either version 3 of the License, or (at your option) any later version.
     10 *
     11 * BOINC is distributed in the hope that it will be useful,
     12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     14 * See the GNU Lesser General Public License for more details.
     15 *
     16 * You should have received a copy of the GNU Lesser General Public License
     17 * along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
     18 ******************************************************************************/
     19package edu.berkeley.boinc.adapter;
     20
     21import edu.berkeley.boinc.R;
     22import android.content.Context;
     23import android.util.Log;
     24
     25public class PrefsListItemWrapperInteger extends PrefsListItemWrapper {
     26       
     27        private final String TAG = "PrefsListItemWrapperInteger";
     28
     29        public String header = "";
     30        public String description = "";
     31        public String unit = "";
     32        public Integer status;
     33       
     34        public PrefsListItemWrapperInteger(Context ctx, Integer ID, Integer categoryID, Integer status) {
     35                super(ctx, ID, categoryID);
     36                this.status = status;
     37                mapStrings(ID);
     38        }
     39       
     40        private void mapStrings(Integer id) {
     41                switch (id) {
     42                case R.string.prefs_cpu_number_cpus_header:
     43                        header = ctx.getString(R.string.prefs_cpu_number_cpus_header);
     44                        description = ctx.getString(R.string.prefs_cpu_number_cpus_description);
     45                        break;
     46                default:
     47                        Log.d(TAG, "map failed!");
     48                }
     49        }
     50}
  • android/BOINC/src/edu/berkeley/boinc/client/ClientStatus.java

    diff --git a/android/BOINC/src/edu/berkeley/boinc/client/ClientStatus.java b/android/BOINC/src/edu/berkeley/boinc/client/ClientStatus.java
    index 0d77830..7357254 100644
    a b import edu.berkeley.boinc.rpc.GlobalPreferences; 
    4343import edu.berkeley.boinc.rpc.Project;
    4444import edu.berkeley.boinc.rpc.Result;
    4545import edu.berkeley.boinc.rpc.Transfer;
     46import edu.berkeley.boinc.rpc.HostInfo;
    4647import edu.berkeley.boinc.utils.BOINCDefs;
    4748
    4849/*
    public class ClientStatus { 
    6566        private ArrayList<Project> projects;
    6667        private ArrayList<Transfer> transfers;
    6768        private GlobalPreferences prefs;
     69        private HostInfo hostinfo;
    6870       
    6971        // setup status
    7072        public Integer setupStatus = 0;
    public class ClientStatus { 
    158160        /*
    159161         * called frequently by Monitor to set the RPC data. These objects are used to determine the client status and parse it in the data model of this class.
    160162         */
    161         public synchronized void setClientStatus(CcStatus status,ArrayList<Result> results,ArrayList<Project> projects, ArrayList<Transfer> transfers) {
     163        public synchronized void setClientStatus(CcStatus status,ArrayList<Result> results,ArrayList<Project> projects, ArrayList<Transfer> transfers, HostInfo hostinfo) {
    162164                this.status = status;
    163165                this.results = results;
    164166                this.projects = projects;
    165167                this.transfers = transfers;
     168                this.hostinfo = hostinfo;
    166169                parseClientStatus();
    167170                Log.d(TAG,"setClientStatus: #results:" + results.size() + " #projects:" + projects.size() + " #transfers:" + transfers.size() + " // computing: " + computingParseError + computingStatus + computingSuspendReason + " - network: " + networkParseError + networkStatus + networkSuspendReason);
    168171                if(!computingParseError && !networkParseError && !setupStatusParseError) {
    public class ClientStatus { 
    229232                }
    230233                return projects;
    231234        }
     235       
     236        public synchronized HostInfo getHostInfo() {
     237                if(hostinfo == null) {
     238                        Log.d(TAG, "getHostInfo() state is null");
     239                        return null;
     240                }
     241                return hostinfo;
     242        }
    232243
    233244        // returns list with slideshow images of all projects
    234245        // 126 * 29 pixel from /projects/PNAME/slideshow_appname_n
  • android/BOINC/src/edu/berkeley/boinc/client/Monitor.java

    diff --git a/android/BOINC/src/edu/berkeley/boinc/client/Monitor.java b/android/BOINC/src/edu/berkeley/boinc/client/Monitor.java
    index ea188ed..990f04c 100644
    a b public class Monitor extends Service { 
    943943                                        if(showRpcCommands) Log.d(TAG, "getTransers");
    944944                                        ArrayList<Transfer>  transfers = rpc.getFileTransfers();
    945945                                       
    946                                         if( (status != null) && (state != null) && (state.results != null) && (state.projects != null) && (transfers != null)) {
    947                                                 Monitor.getClientStatus().setClientStatus(status, state.results, state.projects, transfers);
     946                                        if( (status != null) && (state != null) && (state.results != null) && (state.projects != null) && (transfers != null) && (state.host_info != null)) {
     947                                                Monitor.getClientStatus().setClientStatus(status, state.results, state.projects, transfers, state.host_info);
    948948                                                // Update status bar notification
    949949                                                ClientNotification.getInstance(getApplicationContext()).update();
    950950                                        } else {